Skip to content

Commit

Permalink
Implemented tests for ClassVar.
Browse files Browse the repository at this point in the history
  • Loading branch information
erictraut committed Dec 30, 2023
1 parent a6b4ad6 commit d43dfcc
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 8 deletions.
31 changes: 31 additions & 0 deletions conformance/results/mypy/classes_classvar.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
conformant = "Partial"
notes = """
Internal error if TypeVarTuple is used in ClassVar.
Does not reject use of ParamSpec in ClassVar.
Rejects ClassVar nested in Annotated.
Does not reject use of ClassVar in TypeAlias definition.
Does not infer type of ClassVar from assignment if no type is provided.
"""
output = """
classes_classvar.py:36: error: ClassVar[...] must have at most one type argument [valid-type]
classes_classvar.py:37: error: Invalid type: try using Literal[3] instead? [valid-type]
classes_classvar.py:38: error: Name "var" is not defined [name-defined]
classes_classvar.py:43: error: ClassVar cannot contain type variables [misc]
classes_classvar.py:44: error: ClassVar cannot contain type variables [misc]
classes_classvar.py:50: error: Incompatible types in assignment (expression has type "dict[Never, Never]", variable has type "list[str]") [assignment]
classes_classvar.py:52: error: Name "Final" is not defined [name-defined]
classes_classvar.py:53: error: Invalid type: ClassVar nested inside other type [valid-type]
classes_classvar.py:59: error: Invalid type: ClassVar nested inside other type [valid-type]
classes_classvar.py:61: error: ClassVar can only be used for assignments in class body [misc]
classes_classvar.py:62: error: ClassVar can only be used for assignments in class body [misc]
classes_classvar.py:63: error: ClassVar can only be used for assignments in class body [misc]
classes_classvar.py:65: error: ClassVar can only be used for assignments in class body [misc]
classes_classvar.py:69: error: ClassVar can only be used for assignments in class body [misc]
classes_classvar.py:76: error: Expression is of type "Any", not "float" [assert-type]
classes_classvar.py:100: error: Cannot assign to class variable "stats" via instance [misc]
classes_classvar.py:129: error: Incompatible types in assignment (expression has type "ProtoAImpl", variable has type "ProtoA") [assignment]
classes_classvar.py:129: note: "ProtoAImpl" is missing following "ProtoA" protocol member:
classes_classvar.py:129: note: z
classes_classvar.py:129: note: Protocol member ProtoA.x expected class variable, got instance variable
classes_classvar.py:129: note: Protocol member ProtoA.y expected class variable, got instance variable
"""
2 changes: 1 addition & 1 deletion conformance/results/mypy/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "mypy 1.8.0"
test_duration = 0.4777989387512207
test_duration = 0.7785298824310303
27 changes: 27 additions & 0 deletions conformance/results/pyre/classes_classvar.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
conformant = "Partial"
notes = """
Does not reject use of TypeVar in ClassVar.
Does not reject use of ParamSpec in ClassVar.
Does not reject use of ClassVar as a generic type argument.
Does not reject use of ClassVar in parameter type annotation.
Does not reject use of ClassVar in local variable annotation.
Does not reject use of ClassVar in instance variable annotation.
Does not reject use of ClassVar in return type annotation.
Does not reject use of ClassVar in type alias definition.
"""
output = """
classes_classvar.py:33:0 Uninitialized attribute [13]: Attribute `bad1` is declared in class `ClassA` to have type `typing.Any` but is never initialized.
classes_classvar.py:33:0 Uninitialized attribute [13]: Attribute `bad4` is declared in class `ClassA` to have type `Variable[T]` but is never initialized.
classes_classvar.py:33:0 Uninitialized attribute [13]: Attribute `bad5` is declared in class `ClassA` to have type `typing.List[Variable[T]]` but is never initialized.
classes_classvar.py:33:0 Uninitialized attribute [13]: Attribute `bad6` is declared in class `ClassA` to have type `typing.Callable[classes_classvar.P, typing.Any]` but is never initialized.
classes_classvar.py:36:10 Invalid type parameters [24]: Generic type `CV` expects 1 type parameter, received 2.
classes_classvar.py:37:10 Invalid type [31]: Expression `typing.ClassVar[3]` is not a valid type.
classes_classvar.py:38:13 Unbound name [10]: Name `var` is used but not defined in the current scope.
classes_classvar.py:50:4 Incompatible attribute type [8]: Attribute `bad8` declared in class `ClassA` has type `List[str]` but is used as type `Dict[Variable[_KT], Variable[_VT]]`.
classes_classvar.py:52:10 Unbound name [10]: Name `Final` is used but not defined in the current scope.
classes_classvar.py:63:8 Undefined attribute [16]: `ClassA` has no attribute `xx`.
classes_classvar.py:66:8 Incompatible return type [7]: Expected `CV[int]` but got `int`.
classes_classvar.py:79:0 Uninitialized attribute [13]: Attribute `damage` is declared in class `BasicStarship` to have type `int` but is never initialized.
classes_classvar.py:100:0 Invalid assignment [41]: Assigning to class variable through instance, did you mean to assign to `Starship.stats` instead?
classes_classvar.py:129:0 Incompatible variable type [9]: a is declared to have type `ProtoA` but is used as type `ProtoAImpl`.
"""
2 changes: 1 addition & 1 deletion conformance/results/pyre/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyre 0.9.19"
test_duration = 1.6688978672027588
test_duration = 1.528454065322876
24 changes: 24 additions & 0 deletions conformance/results/pyright/classes_classvar.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
conformant = "Pass"
output = """
classes_classvar.py:36:25 - error: Expected only one type argument after "ClassVar"
classes_classvar.py:37:14 - error: Expected type expression but received "Literal[3]" (reportGeneralTypeIssues)
classes_classvar.py:38:14 - error: "var" is not defined (reportUndefinedVariable)
classes_classvar.py:43:20 - error: "ClassVar" type cannot include type variables (reportGeneralTypeIssues)
classes_classvar.py:44:20 - error: "ClassVar" type cannot include type variables (reportGeneralTypeIssues)
classes_classvar.py:45:20 - error: "ClassVar" type cannot include type variables (reportGeneralTypeIssues)
classes_classvar.py:50:33 - error: Expression of type "dict[Any, Any]" cannot be assigned to declared type "list[str]" (reportGeneralTypeIssues)
classes_classvar.py:52:11 - error: "Final" is not defined (reportUndefinedVariable)
classes_classvar.py:53:17 - error: "ClassVar" is not allowed in this context
classes_classvar.py:61:26 - error: "ClassVar" is not allowed in this context
classes_classvar.py:62:12 - error: "ClassVar" is not allowed in this context
classes_classvar.py:63:18 - error: "ClassVar" is not allowed in this context
classes_classvar.py:65:26 - error: "ClassVar" is not allowed in this context
classes_classvar.py:69:8 - error: "ClassVar" is not allowed in this context
classes_classvar.py:70:20 - error: "ClassVar" is not allowed in this context
classes_classvar.py:100:14 - error: Cannot assign member "stats" for type "Starship"
  Member "stats" cannot be assigned through a class instance because it is a ClassVar
    Member "__set__" is unknown (reportGeneralTypeIssues)
classes_classvar.py:129:13 - error: Expression of type "ProtoAImpl" cannot be assigned to declared type "ProtoA"
  "ProtoAImpl" is incompatible with protocol "ProtoA"
    "y" is not a class variable (reportGeneralTypeIssues)
"""
2 changes: 1 addition & 1 deletion conformance/results/pyright/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyright 1.1.344"
test_duration = 1.0010731220245361
test_duration = 1.0201241970062256
45 changes: 45 additions & 0 deletions conformance/results/pytype/classes_classvar.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
conformant = "Partial"
notes = """
Does not reject use of TypeVar in ClassVar.
Does not reject use of ParamSpec in ClassVar.
Does not reject use of ClassVar as a generic type argument.
Rejects initialization of ClassVar if no type argument is provided.
Does not reject use of ClassVar in parameter type annotation.
Does not reject use of ClassVar in local variable annotation.
Does not reject use of ClassVar in instance variable annotation.
Does not reject use of ClassVar in return type annotation.
Does not reject use of ClassVar in type alias definition.
Does not reject assignment of ClassVar through instance of class.
"""
output = """
File "classes_classvar.py", line 7, in <module>: typing.TypeVarTuple not supported yet [not-supported-yet]
File "classes_classvar.py", line 27, in <module>: Function TypeVarTuple.__init__ expects 1 arg(s), got 2 [wrong-arg-count]
Expected: (self)
Actually passed: (self, _)
File "classes_classvar.py", line 33, in <module>: Invalid type annotation 'Generic' [invalid-annotation]
Parameters to Generic[...] must all be type variables
File "classes_classvar.py", line 36, in ClassA: Invalid type annotation 'ClassVar[int, str]' [invalid-annotation]
ClassVar[_T] expected 1 parameter, got 2
File "classes_classvar.py", line 37, in ClassA: class ClassVar is not indexable [not-indexable]
File "classes_classvar.py", line 38, in ClassA: Name 'var' is not defined [name-error]
File "classes_classvar.py", line 50, in ClassA: Type annotation for bad8 does not match type of assignment [annotation-type-mismatch]
Annotation: List[str]
Assignment: Dict[nothing, nothing]
File "classes_classvar.py", line 52, in ClassA: Name 'Final' is not defined [name-error]
File "classes_classvar.py", line 58, in ClassA: Type annotation for good4 does not match type of assignment [annotation-type-mismatch]
Annotation: ClassVar
Assignment: float
File "classes_classvar.py", line 66, in method2: bad return type [bad-return-type]
Expected: ClassVar[int]
Actually returned: int
File "classes_classvar.py", line 76, in <module>: ClassVar [assert-type]
Expected: float
Actual: ClassVar
File "classes_classvar.py", line 119, in ProtoA: Type annotation for z does not match type of assignment [annotation-type-mismatch]
Annotation: ClassVar
Assignment: List[str]
File "classes_classvar.py", line 129, in <module>: Type annotation for a does not match type of assignment [annotation-type-mismatch]
Annotation: ProtoA
Assignment: ProtoAImpl
Attributes of protocol ProtoA are not implemented on ProtoAImpl: z
"""
2 changes: 1 addition & 1 deletion conformance/results/pytype/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pytype 2023.12.18"
test_duration = 54.82685399055481
test_duration = 81.08336782455444
Loading

0 comments on commit d43dfcc

Please sign in to comment.