Skip to content
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

Fix tests on Python 3.12 #162

Merged
merged 9 commits into from
May 20, 2023
Merged

Fix tests on Python 3.12 #162

merged 9 commits into from
May 20, 2023

Conversation

JelleZijlstra
Copy link
Member

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 wraps typing.TypeVar and when instantiated, returns an instance of typing.TypeVar, possibly with some additional attributes. We also set __instancecheck__ so that isinstance(x, typing_extensions.TypeVar) continues to work.

Also fix a few other minor test failures on current CPython main.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

src/test_typing_extensions.py Outdated Show resolved Hide resolved
src/test_typing_extensions.py Show resolved Hide resolved
src/test_typing_extensions.py Outdated Show resolved Hide resolved
class TypeVar(metaclass=_TypeVarMeta):
"""Type variable."""

__module__ = 'typing'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh

JelleZijlstra and others added 2 commits May 19, 2023 17:20
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstra
Copy link
Member Author

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.

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 typing classes.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

class Foo[T](Protocol):
def meth(self, x: T) -> None: ...

(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:

if sys.version_info >= (3, 12):

But also, the beta is about to be released, so maybe it doesn't matter too much?

@JelleZijlstra
Copy link
Member Author

Thanks! I'll push something to make it check for hasattr(typing, 'TypeAliasType') instead. I remember @carljm saying at the language summit that it's impossible to test any third-party libraries on early alphas; maybe we can make typing-extensions a partial exception :)

@JelleZijlstra JelleZijlstra merged commit 8b6582e into python:main May 20, 2023
15 checks passed
@JelleZijlstra JelleZijlstra deleted the 312 branch May 20, 2023 23:29
ghickman added a commit to opensafely-core/job-server that referenced this pull request Oct 9, 2023
This brings in a fix for python 3.12:

python/typing_extensions#162
benjamb added a commit to benjamb/mypy that referenced this pull request Jun 2, 2024
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.
JelleZijlstra pushed a commit to python/mypy that referenced this pull request Jun 2, 2024
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.
GiorgosPapoutsakis pushed a commit to GiorgosPapoutsakis/mypy that referenced this pull request Jun 3, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants