-
Notifications
You must be signed in to change notification settings - Fork 263
Conformance tests and spec change for generic type erasure #1589
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Conformance tests and spec change for generic type erasure
- Loading branch information
commit cd81f97c1ed5702f18c826e5b22ee716f41c580d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#instantiating-generic-classes-and-type-erasure | ||
|
||
from typing import Any, TypeVar, Generic, assert_type | ||
|
||
T = TypeVar("T") | ||
|
||
# > If the constructor (__init__ or __new__) uses T in its signature, and a | ||
# > corresponding argument value is passed, the type of the corresponding | ||
# > argument(s) is substituted. Otherwise, Any is assumed. | ||
|
||
class Node(Generic[T]): | ||
label: T | ||
def __init__(self, label: T | None = None) -> None: ... | ||
|
||
assert_type(Node(''), Node[str]) | ||
assert_type(Node(0), Node[int]) | ||
assert_type(Node(), Node[Any]) | ||
|
||
assert_type(Node(0).label, int) | ||
assert_type(Node().label, Any) | ||
|
||
# > In case the inferred type uses [Any] but the intended type is more specific, | ||
# > you can use an annotation to force the type of the variable, e.g.: | ||
|
||
n1: Node[int] = Node() | ||
assert_type(n1, Node[int]) | ||
n2: Node[str] = Node() | ||
assert_type(n2, Node[str]) | ||
|
||
n3 = Node[int]() | ||
assert_type(n3, Node[int]) | ||
n4 = Node[str]() | ||
assert_type(n4, Node[str]) | ||
|
||
n5 = Node[int](0) # OK | ||
n6 = Node[int]("") # Type error | ||
n7 = Node[str]("") # OK | ||
n8 = Node[str](0) # Type error | ||
|
||
Node[int].label = 1 # Type error | ||
Node[int].label # Type error | ||
Node.label = 1 # Type error | ||
Node.label # Type error | ||
type(n1).label # Type error | ||
assert_type(n1.label, int) | ||
assert_type(Node[int]().label, int) | ||
n1.label = 1 # OK | ||
|
||
# > [...] generic versions of concrete collections can be instantiated: | ||
|
||
from typing import DefaultDict | ||
|
||
data = DefaultDict[int, bytes]() | ||
assert_type(data[0], bytes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -333,15 +333,15 @@ argument(s) is substituted. Otherwise, ``Any`` is assumed. Example:: | |
|
||
class Node(Generic[T]): | ||
x: T # Instance attribute (see below) | ||
def __init__(self, label: T = None) -> None: | ||
def __init__(self, label: T | None = None) -> None: | ||
... | ||
|
||
x = Node('') # Inferred type is Node[str] | ||
y = Node(0) # Inferred type is Node[int] | ||
z = Node() # Inferred type is Node[Any] | ||
|
||
In case the inferred type uses ``[Any]`` but the intended type is more | ||
specific, you can use a type comment (see below) to force the type of | ||
specific, you can use an annotation (see below) to force the type of | ||
the variable, e.g.:: | ||
|
||
# (continued from previous example) | ||
|
@@ -373,8 +373,8 @@ class instance that does not have an instance attribute with the same name:: | |
Node.x = 1 # Error | ||
Node.x # Error | ||
type(p).x # Error | ||
p.x # Ok (evaluates to None) | ||
Node[int]().x # Ok (evaluates to None) | ||
p.x # Ok (evaluates to int) | ||
Node[int]().x # Ok (evaluates to int) | ||
p.x = 1 # Ok, but assigning to instance attribute | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These just seem straight up incorrect to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree. Looks like a spec bug. |
||
|
||
Generic versions of abstract collections like ``Mapping`` or ``Sequence`` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The paragraph above could probably be improved. For instance, without the change I make here, pyright would infer
Node()
to beNode[None]
, and I think that should be legal.That said, handling of default values for generics in general is underspecified (mypy handles them poorly, but in part because it wasn't clear what the behaviour should be python/mypy#3737)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this part of the spec was written, mypy defaulted to "implicit-optional", which would give you the same effect as adding
| None
. Since then, the spec was updated to recommend against "implicit-optional", but this code sample wasn't updated. I'd be in favor of just updating the spec to fix the sample.