Skip to content

(🎁) conformance: NewType isn't a class #2015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions conformance/results/mypy/aliases_newtype.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
conformant = "Pass"
conformant = "Partial"
notes = """
`NewType`s are considered classes, not functions.
"""
output = """
aliases_newtype.py:11: error: Argument 1 to "UserId" has incompatible type "str"; expected "int" [arg-type]
aliases_newtype.py:12: error: Incompatible types in assignment (expression has type "int", variable has type "UserId") [assignment]
aliases_newtype.py:20: error: Cannot use isinstance() with NewType type [misc]
aliases_newtype.py:23: error: Cannot subclass "NewType" [misc]
aliases_newtype.py:32: error: String argument 1 "BadName" to NewType(...) does not match variable name "GoodName" [misc]
aliases_newtype.py:38: error: "GoodNewType1" expects no type arguments, but 1 given [type-arg]
aliases_newtype.py:44: error: Argument 2 to NewType(...) must be subclassable (got "int | str") [valid-newtype]
aliases_newtype.py:47: error: Type variable "aliases_newtype.T" is unbound [valid-type]
aliases_newtype.py:47: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
aliases_newtype.py:47: note: (Hint: Use "T" in function signature to bind "T" inside a function)
aliases_newtype.py:49: error: NewType cannot be used with protocol classes [misc]
aliases_newtype.py:51: error: Argument 2 to NewType(...) must be subclassable (got "Literal[7]") [valid-newtype]
aliases_newtype.py:58: error: Argument 2 to NewType(...) must be subclassable (got "TD1") [valid-newtype]
aliases_newtype.py:60: error: NewType(...) expects exactly two positional arguments [misc]
aliases_newtype.py:62: error: Argument 2 to NewType(...) must be subclassable (got "Any") [valid-newtype]
aliases_newtype.py:23: error: Cannot use isinstance() with NewType type [misc]
aliases_newtype.py:26: error: Cannot subclass "NewType" [misc]
aliases_newtype.py:35: error: String argument 1 "BadName" to NewType(...) does not match variable name "GoodName" [misc]
aliases_newtype.py:41: error: "GoodNewType1" expects no type arguments, but 1 given [type-arg]
aliases_newtype.py:47: error: Argument 2 to NewType(...) must be subclassable (got "int | str") [valid-newtype]
aliases_newtype.py:50: error: Type variable "aliases_newtype.T" is unbound [valid-type]
aliases_newtype.py:50: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
aliases_newtype.py:50: note: (Hint: Use "T" in function signature to bind "T" inside a function)
aliases_newtype.py:52: error: NewType cannot be used with protocol classes [misc]
aliases_newtype.py:54: error: Argument 2 to NewType(...) must be subclassable (got "Literal[7]") [valid-newtype]
aliases_newtype.py:61: error: Argument 2 to NewType(...) must be subclassable (got "TD1") [valid-newtype]
aliases_newtype.py:63: error: NewType(...) expects exactly two positional arguments [misc]
aliases_newtype.py:65: error: Argument 2 to NewType(...) must be subclassable (got "Any") [valid-newtype]
"""
conformance_automated = "Pass"
conformance_automated = "Fail"
errors_diff = """
Line 18: Expected 1 errors
"""
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.15.0"
test_duration = 1.5
test_duration = 1.4
30 changes: 17 additions & 13 deletions conformance/results/pyright/aliases_newtype.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
conformant = "Pass"
conformant = "Partial"
notes = """
`NewType`s are considered classes, not functions.
"""
output = """
aliases_newtype.py:11:8 - error: Argument of type "Literal['user']" cannot be assigned to parameter "_x" of type "int" in function "__init__"
  "Literal['user']" is not assignable to "int" (reportArgumentType)
aliases_newtype.py:12:14 - error: Type "Literal[42]" is not assignable to declared type "UserId"
  "Literal[42]" is not assignable to "UserId" (reportAssignmentType)
aliases_newtype.py:20:16 - error: Second argument to "isinstance" must be a class or tuple of classes
aliases_newtype.py:23:16 - error: Second argument to "isinstance" must be a class or tuple of classes
  Class created with NewType cannot be used with instance and class checks (reportArgumentType)
aliases_newtype.py:23:21 - error: Base class "UserId" is marked final and cannot be subclassed (reportGeneralTypeIssues)
aliases_newtype.py:32:1 - error: NewType must be assigned to a variable with the same name (reportGeneralTypeIssues)
aliases_newtype.py:38:19 - error: Expected no type arguments for class "GoodNewType1" (reportInvalidTypeArguments)
aliases_newtype.py:44:38 - error: Expected class as second argument to NewType (reportGeneralTypeIssues)
aliases_newtype.py:47:43 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
aliases_newtype.py:49:38 - error: NewType cannot be used with structural type (a Protocol or TypedDict class) (reportGeneralTypeIssues)
aliases_newtype.py:51:38 - error: NewType cannot be used with Literal type (reportGeneralTypeIssues)
aliases_newtype.py:58:38 - error: NewType cannot be used with structural type (a Protocol or TypedDict class) (reportGeneralTypeIssues)
aliases_newtype.py:60:15 - error: NewType requires two positional arguments (reportCallIssue)
aliases_newtype.py:62:38 - error: The second argument to NewType must be a known class, not Any or Unknown (reportGeneralTypeIssues)
aliases_newtype.py:26:21 - error: Base class "UserId" is marked final and cannot be subclassed (reportGeneralTypeIssues)
aliases_newtype.py:35:1 - error: NewType must be assigned to a variable with the same name (reportGeneralTypeIssues)
aliases_newtype.py:41:19 - error: Expected no type arguments for class "GoodNewType1" (reportInvalidTypeArguments)
aliases_newtype.py:47:38 - error: Expected class as second argument to NewType (reportGeneralTypeIssues)
aliases_newtype.py:50:43 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
aliases_newtype.py:52:38 - error: NewType cannot be used with structural type (a Protocol or TypedDict class) (reportGeneralTypeIssues)
aliases_newtype.py:54:38 - error: NewType cannot be used with Literal type (reportGeneralTypeIssues)
aliases_newtype.py:61:38 - error: NewType cannot be used with structural type (a Protocol or TypedDict class) (reportGeneralTypeIssues)
aliases_newtype.py:63:15 - error: NewType requires two positional arguments (reportCallIssue)
aliases_newtype.py:65:38 - error: The second argument to NewType must be a known class, not Any or Unknown (reportGeneralTypeIssues)
"""
conformance_automated = "Pass"
conformance_automated = "Fail"
errors_diff = """
Line 18: Expected 1 errors
"""
4 changes: 2 additions & 2 deletions conformance/results/pyright/aliases_typealiastype.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ aliases_typealiastype.py:61:42 - error: Expected class but received "Literal[Tru
aliases_typealiastype.py:62:42 - error: Expected class but received "Literal[1]" (reportGeneralTypeIssues)
aliases_typealiastype.py:63:42 - error: Binary operator not allowed in type expression (reportInvalidTypeForm)
aliases_typealiastype.py:64:42 - error: Type expressions cannot use format string literals (f-strings) (reportGeneralTypeIssues)
aliases_typealiastype.py:66:47 - error: "BadAlias21" is not defined (reportUndefinedVariable)
"""
conformance_automated = "Fail"
conformance_automated = "Pass"
errors_diff = """
Line 66: Expected 1 errors
"""
7 changes: 3 additions & 4 deletions conformance/results/pyright/callables_protocol.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ callables_protocol.py:70:7 - error: Type "(**b: str) -> None" is not assignable
  Type "(**b: str) -> None" is not assignable to type "(*vals: bytes, **kwargs: str) -> None"
    Parameter "*vals" has no corresponding parameter (reportAssignmentType)
callables_protocol.py:97:16 - error: Type "(x: int) -> None" is not assignable to declared type "Proto4"
  "function" is incompatible with protocol "Proto4"
    "other_attribute" is not present
    "__call__" is not present (reportAssignmentType)
  "FunctionType" is incompatible with protocol "Proto4"
    "other_attribute" is not present (reportAssignmentType)
callables_protocol.py:121:18 - error: Type "(*vals: bytes, max_len: int | None = None) -> list[bytes]" is not assignable to declared type "NotProto6"
  "function" is not assignable to "NotProto6" (reportAssignmentType)
  "FunctionType" is not assignable to "NotProto6" (reportAssignmentType)
callables_protocol.py:169:7 - error: Type "(x: int) -> Any" is not assignable to declared type "Proto8"
  One or more overloads of "__call__" is not assignable
    Type "(x: int) -> Any" is not assignable to type "(x: str) -> str"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ generics_syntax_infer_variance.py:29:35 - error: Type "ShouldBeCovariant1[float]
  "ShouldBeCovariant1[float]" is not assignable to "ShouldBeCovariant1[int]"
    Type parameter "T@ShouldBeCovariant1" is covariant, but "float" is not a subtype of "int"
      "float" is not assignable to "int" (reportAssignmentType)
generics_syntax_infer_variance.py:36:37 - error: Cannot instantiate abstract class "ShouldBeCovariant2"
  "Collection.__len__" is not implemented
  "Sequence.__getitem__" is not implemented (reportAbstractUsage)
generics_syntax_infer_variance.py:37:35 - error: Cannot instantiate abstract class "ShouldBeCovariant2"
  "Collection.__len__" is not implemented
  "Sequence.__getitem__" is not implemented (reportAbstractUsage)
generics_syntax_infer_variance.py:37:35 - error: Type "ShouldBeCovariant2[float]" is not assignable to declared type "ShouldBeCovariant2[int]"
  "ShouldBeCovariant2[float]" is not assignable to "ShouldBeCovariant2[int]"
    Type parameter "T@ShouldBeCovariant2" is covariant, but "float" is not a subtype of "int"
Expand Down Expand Up @@ -57,6 +63,7 @@ generics_syntax_infer_variance.py:155:45 - error: Type "ShouldBeContravariant1[i
    Type parameter "T@ShouldBeContravariant1" is contravariant, but "int" is not a supertype of "float"
      "float" is not assignable to "int" (reportAssignmentType)
"""
conformance_automated = "Pass"
conformance_automated = "Fail"
errors_diff = """
Line 36: Unexpected errors ['generics_syntax_infer_variance.py:36:37 - error: Cannot instantiate abstract class "ShouldBeCovariant2"']
"""
2 changes: 1 addition & 1 deletion conformance/results/pyright/protocols_definition.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protocols_definition.py:160:22 - error: Type "Concrete3_Bad5" is not assignable
protocols_definition.py:218:22 - error: Type "Concrete4_Bad1" is not assignable to declared type "Template4"
  "Concrete4_Bad1" is incompatible with protocol "Template4"
    "val1" is an incompatible type
      "function" is not assignable to "Sequence[float]" (reportAssignmentType)
      "FunctionType" is not assignable to "Sequence[float]" (reportAssignmentType)
protocols_definition.py:219:22 - error: Type "Concrete4_Bad2" is not assignable to declared type "Template4"
  "Concrete4_Bad2" is incompatible with protocol "Template4"
    "val1" is not present (reportAssignmentType)
Expand Down
4 changes: 2 additions & 2 deletions conformance/results/pyright/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyright 1.1.400"
test_duration = 1.1
version = "pyright 1.1.401"
test_duration = 1.0
12 changes: 6 additions & 6 deletions conformance/results/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ <h3>Python Type System Conformance Test Results</h3>
<div class="table_container"><table><tbody>
<tr><th class="col1">&nbsp;</th>
<th class='tc-header'><div class='tc-name'>mypy 1.15.0</div>
<div class='tc-time'>1.5sec</div>
<div class='tc-time'>1.4sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyright 1.1.400</div>
<div class='tc-time'>1.1sec</div>
<th class='tc-header'><div class='tc-name'>pyright 1.1.401</div>
<div class='tc-time'>1.0sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyre 0.9.23</div>
<div class='tc-time'>10.7sec</div>
<div class='tc-time'>0.6sec</div>
</th>
</tr>
<tr><th class="column" colspan="4">
Expand Down Expand Up @@ -422,8 +422,8 @@ <h3>Python Type System Conformance Test Results</h3>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Incorrectly reports error for type alias defined with ParamSpec.</p><p>Incorrectly rejects some valid type aliases when used in annotations.</p><p>Incorrectly evaluates generic type alias with ParamSpec with missing type argument.</p><p>Does not report invalid specialization of generic type aliases.</p><p>Does not report error for attempt to instantiate union type alias.</p><p>Does not report invalid specialization of already-specialized generic type alias.</p></span></div></th>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aliases_newtype</th>
<th class="column col2 conformant">Pass</th>
<th class="column col2 conformant">Pass</th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>`NewType`s are considered classes, not functions.</p></span></div></th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>`NewType`s are considered classes, not functions.</p></span></div></th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not reject use of NewType in `isinstance` call.</p><p>Does not reject use of NewType in class definition statement.</p><p>Does not report inconsistency between name of NewType and assigned identifier name.</p><p>Does not reject use of NewType with generic class with TypeVar.</p><p>Does not reject use of NewType with protocol class.</p><p>Does not reject use of NewType with TypedDict class.</p><p>Does not reject use of NewType with Any.</p></span></div></th>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aliases_recursive</th>
Expand Down
3 changes: 3 additions & 0 deletions conformance/tests/aliases_newtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

assert_type(UserId(5) + 1, int)

# > NewType('Derived', Base) returns a dummy function
_: type = UserId # E: functions are not instances of `type`

# > Both isinstance and issubclass, as well as subclassing will fail for
# > NewType('Derived', Base) since function objects don’t support these
# > operations.
Expand Down