Conversation
AlexWaygood
left a comment
There was a problem hiding this comment.
The code looks good! A few comments about the tests. The only big concern I have is about typing.Unpack -- I think we should backport python/cpython#104048 rather than just fiddling with the test to make it pass on 3.12.
Feels like we could probably share more code between _TypeVarMeta, _ParamSpecMeta and _TypeVarTupleMeta, but that can definitely be tackled in a followup PR; no need to worry about it now.
| class TypeVar(metaclass=_TypeVarMeta): | ||
| """Type variable.""" | ||
|
|
||
| __module__ = 'typing' |
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
I'm not sure that's feasible. The code is similar for all three, but not quite the same: they all take different arguments and map to different |
There was a problem hiding this comment.
The changes look good, and I confirmed that the tests all pass on CPython main.
It looks like the tests do currently crash on 3.12 alpha7, due to this not being valid syntax until very recently:
typing_extensions/src/test_typing_extensions.py
Lines 2404 to 2405 in 40dbc09
(See https://github.com/python/typing_extensions/actions/runs/5029362366/jobs/9020946297?pr=162)
So I suppose we could possibly make this sys.version_info check more fine-grained:
typing_extensions/src/test_typing_extensions.py
Line 2399 in 40dbc09
But also, the beta is about to be released, so maybe it doesn't matter too much?
|
Thanks! I'll push something to make it check for |
This brings in a fix for python 3.12: python/typing_extensions#162
With earlier versions of typing_extensions, the following traceback is
seen:
```
Traceback (most recent call last):
File ".../bin/mypy", line 5, in <module>
from mypy.__main__ import console_entry
File ".../lib/python3.12/site-packages/mypy/__main__.py", line 9, in <module>
from mypy.main import main, process_options
File ".../lib/python3.12/site-packages/mypy/main.py", line 12, in <module>
from typing_extensions import Final
File ".../lib/python3.12/site-packages/typing_extensions.py", line 1174, in <module>
class TypeVar(typing.TypeVar, _DefaultMixin, _root=True):
TypeError: type 'typing.TypeVar' is not an acceptable base type
```
The error is addressed in typing_extensions in
python/typing_extensions#162, which is included
in the 4.6.0 release.
…n#17312) With earlier versions of typing_extensions, the following traceback is seen: ``` Traceback (most recent call last): File ".../bin/mypy", line 5, in <module> from mypy.__main__ import console_entry File ".../lib/python3.12/site-packages/mypy/__main__.py", line 9, in <module> from mypy.main import main, process_options File ".../lib/python3.12/site-packages/mypy/main.py", line 12, in <module> from typing_extensions import Final File ".../lib/python3.12/site-packages/typing_extensions.py", line 1174, in <module> class TypeVar(typing.TypeVar, _DefaultMixin, _root=True): TypeError: type 'typing.TypeVar' is not an acceptable base type ``` The error is addressed in typing_extensions in python/typing_extensions#162, which is included in the 4.6.0 release.
On current Python main, TypeVar cannot be subclassed. However, we still need to enhance TypeVar to add the
default=argument. This PR introduces an alternative approach to achieve that: we create a dummy class that wrapstyping.TypeVarand when instantiated, returns an instance oftyping.TypeVar, possibly with some additional attributes. We also set__instancecheck__so thatisinstance(x, typing_extensions.TypeVar)continues to work.Also fix a few other minor test failures on current CPython main.