-
Notifications
You must be signed in to change notification settings - Fork 258
TypeVar equality broken? #512
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
Comments
FWIW this is actually intentional. I think it was discussed briefly on this tracker, the main motivation is that from the point of view of static type checkers two type variables even with the same name can be different, they follow special scoping rules specified by PEP 484, for example: T = TypeVar('T')
def f(x: T) -> T: ... # the two T's here are the same
def g(x: T) -> T: ... # the two T's here are the same, but different from the two T's above Also this is how type variables work in some other statically typed languages with generics/templates/etc. They get (re-)bound by a class or function definition. I however don't have any strong opinion on their runtime behavior. @gvanrossum may add to this. |
Why would you ever want to compare TypeVars?
|
This occurred during unit testing. |
Sorry, a bit more context: I was trying to add serialization support for typing hints in cloudpickle (see issue #511), and tried to use |
Hm, I'd expect a TypeVar to be pickled as a reference, not as a value (just
like references to classes and functions). Though for that to work we'd
need to add __module__ to TypeVars.
|
If we all agree that type variables should be treated as "immutable", i.e. |
There is no API to mutate a TypeVar and IMO there shouldn't be -- conceptually it really is just a token. So I agree. Hopefully this is acceptable to Antoine? |
Yes, it sounds ok. |
… by copy and pickle (pythonGH-6216) This also fixes python/typingGH-512 This also fixes python/typingGH-511 As was discussed in both issues, some typing forms deserve to be treated as immutable by copy and pickle modules, so that: * copy(X) is X * deepcopy(X) is X * loads(dumps(X)) is X GH- pickled by reference This PR adds such behaviour to: * Type variables * Special forms like Union, Any, ClassVar * Unsubscripted generic aliases to containers like List, Mapping, Iterable This not only resolves inconsistencies mentioned in the issues, but also improves backwards compatibility with previous versions of Python (including 3.6). Note that this requires some dances with __module__ for type variables (similar to NamedTuple) because the class TypeVar itself is define in typing, while type variables should get module where they were defined. https://bugs.python.org/issue32873 (cherry picked from commit 8349403) Co-authored-by: Ivan Levkivskyi <[email protected]>
… by copy and pickle (GH-6216) This also fixes python/typing#512 This also fixes python/typing#511 As was discussed in both issues, some typing forms deserve to be treated as immutable by copy and pickle modules, so that: * copy(X) is X * deepcopy(X) is X * loads(dumps(X)) is X # pickled by reference This PR adds such behaviour to: * Type variables * Special forms like Union, Any, ClassVar * Unsubscripted generic aliases to containers like List, Mapping, Iterable This not only resolves inconsistencies mentioned in the issues, but also improves backwards compatibility with previous versions of Python (including 3.6). Note that this requires some dances with __module__ for type variables (similar to NamedTuple) because the class TypeVar itself is define in typing, while type variables should get module where they were defined. https://bugs.python.org/issue32873
… by copy and pickle (GH-6216) This also fixes python/typingGH-512 This also fixes python/typingGH-511 As was discussed in both issues, some typing forms deserve to be treated as immutable by copy and pickle modules, so that: * copy(X) is X * deepcopy(X) is X * loads(dumps(X)) is X GH- pickled by reference This PR adds such behaviour to: * Type variables * Special forms like Union, Any, ClassVar * Unsubscripted generic aliases to containers like List, Mapping, Iterable This not only resolves inconsistencies mentioned in the issues, but also improves backwards compatibility with previous versions of Python (including 3.6). Note that this requires some dances with __module__ for type variables (similar to NamedTuple) because the class TypeVar itself is define in typing, while type variables should get module where they were defined. https://bugs.python.org/issue32873 (cherry picked from commit 8349403) Co-authored-by: Ivan Levkivskyi <[email protected]>
This looks unexpected to me:
The text was updated successfully, but these errors were encountered: