python / typing_extensions

Backported and experimental type hints for Python
Other
446 stars 110 forks source link

Fix most tests on Python 3.13 #378

Closed AlexWaygood closed 6 months ago

AlexWaygood commented 6 months ago

Helps with #377. Lots of changes to reflect changes in the PEP-696 implementation that were made upstream but had yet to be backported here.

There's still a couple of failing tests on Python 3.13 even with this PR, but this gets us a lot closer to a clean bill of health.

AlexWaygood commented 6 months ago

@JelleZijlstra any idea what's up with the pickle test failing? It's only failing on Python 3.13, which suggests it might be an issue with the CPython implementation, and I can repro locally in my CPython clone:

(main) % ./python.exe                                                                                                                ~/dev/cpython
Python 3.14.0a0 (heads/main:66f8bb76a1, May  8 2024, 17:00:15) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import NoDefault
>>> import pickle
>>> pickle.dumps(NoDefault)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    pickle.dumps(NoDefault)
    ~~~~~~~~~~~~^^^^^^^^^^^
_pickle.PicklingError: can't pickle 'NoDefaultType' object: typing.NoDefault
>>> import typing
>>> pickle.dumps(typing.NoDefault)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    pickle.dumps(typing.NoDefault)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
_pickle.PicklingError: can't pickle 'NoDefaultType' object: typing.NoDefault

What I don't understand is why the pickling tests are passing in the CPython test suite: https://github.com/python/cpython/blob/7ac933e2609b2ef9b08ccf9c815b682b0e1ede2a/Lib/test/test_typing.py#L10230-L10234

JelleZijlstra commented 6 months ago

The pickle thing is weird; I can reproduce it on CPython when I comment out the tests.addTests(doctest.DocTestSuite(typing)) call at the bottom of test_typing.py. Something must have a side effect but I'm not sure what.

AlexWaygood commented 6 months ago

The pickle thing is weird; I can reproduce it on CPython when I comment out the tests.addTests(doctest.DocTestSuite(typing)) call at the bottom of test_typing.py. Something must have a side effect but I'm not sure what.

That is weird. I guess I'll skip the test over here for now.

JelleZijlstra commented 6 months ago

Found it:

% ./python.exe
Python 3.14.0a0 (heads/main:7ac933e260, May 10 2024, 05:30:33) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing, pickle
>>> pickle.dumps(typing.NoDefault)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    pickle.dumps(typing.NoDefault)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
_pickle.PicklingError: can't pickle 'NoDefaultType' object: typing.NoDefault
>>> typing.NoDefault.__doc__
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    typing.NoDefault.__doc__
AttributeError: 'NoDefaultType' object has no attribute '__doc__'. Did you mean: '__dir__'?
>>> pickle.dumps(typing.NoDefault)
b'\x80\x04\x95\x19\x00\x00\x00\x00\x00\x00\x00\x8c\x07_typing\x94\x8c\tNoDefault\x94\x93\x94.'
>>> typing.NoDefault.__doc__
'The type of the NoDefault singleton.'

The good news is that I think python/cpython#118897 fixes this. (Nikita just commented as much.)

AlexWaygood commented 6 months ago

Alright, so just one failing test left now:

======================================================================
FAIL: test_async_contextmanager (test_typing_extensions.OtherABCTests.test_async_contextmanager)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/typing_extensions/typing_extensions/src/test_typing_extensions.py", line 1652, in test_async_contextmanager
    self.assertEqual(typing_extensions.AsyncContextManager[int].__args__, (int,))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Tuples differ: (<class 'int'>, bool | None) != (<class 'int'>,)

First tuple contains 1 additional elements.
First extra element 1:
bool | None

- (<class 'int'>, bool | None)
+ (<class 'int'>,)

The principled way of fixing that is to make typing_extensions.AsyncContextManager have an optional second type parameter, the same as typing.AsyncContextManager on py313+. But I think doing that might possibly involve backporting the whole typing._GenericAlias implementation from CPython, which I don't really wanna do in this PR.

JelleZijlstra commented 6 months ago

Let's merge this now to fix a number of the tests; we can fix the rest separately.

erlend-aasland commented 6 months ago

Thanks for fixing these! Currently, the canopen test suite fails to run on Python 3.13; looks like this should do the trick. Is there a 4.11.1 release scheduled?

AlexWaygood commented 6 months ago

Thanks for fixing these! Currently, the canopen test suite fails to run on Python 3.13; looks like this should do the trick. Is there a 4.11.1 release scheduled?

If we cut a new release right now, it would break pydantic, which would be very bad (https://github.com/python/typing_extensions/issues/381) -- we've broken pydantic before, and the experience was extremely chaotic for us, we definitely don't want to do it again 🙃

I've opened a PR to fix pydantic's tests with the typing_extensions main branch here: https://github.com/pydantic/pydantic/pull/9426.

erlend-aasland commented 6 months ago

Great! I'm watching https://github.com/pydantic/pydantic/pull/9426 and keeping an eye out for new releases of typing-extensions :)

AlexWaygood commented 6 months ago

typing_extensions 4.12.0rc1 is now out @erlend-aasland! :D https://pypi.org/project/typing-extensions/4.12.0rc1/

JelleZijlstra commented 6 months ago

The final release should happen in a week (next Thursday).