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

Weird import conflict with xarray and typing-extensions on Python 3.8 #99

Closed
soxofaan opened this issue Jun 6, 2024 · 3 comments
Closed

Comments

@soxofaan
Copy link

soxofaan commented Jun 6, 2024

One of our CI builds suddenly started failing with this stack trace:

tests/util/test_logging.py:1: in <module>
    import dirty_equals
venv38/lib64/python3.8/site-packages/dirty_equals/__init__.py:38: in <module>
    from ._sequence import Contains, HasLen, IsList, IsListOrTuple, IsTuple
venv38/lib64/python3.8/site-packages/dirty_equals/_sequence.py:215: in <module>
    class IsList(IsListOrTuple[List[Any]]):
/usr/lib64/python3.8/typing.py:258: in inner
    return cached(*args, **kwds)
/usr/lib64/python3.8/typing.py:895: in __class_getitem__
    return _GenericAlias(cls, params)
/usr/lib64/python3.8/typing.py:669: in __init__
    self.__parameters__ = _collect_type_vars(params)
venv38/lib64/python3.8/site-packages/typing_extensions.py:2994: in _collect_type_vars
    enforce_default_ordering = _has_generic_or_protocol_as_origin()
venv38/lib64/python3.8/site-packages/typing_extensions.py:2961: in _has_generic_or_protocol_as_origin
    return frame.f_locals.get("origin") in (
venv38/lib64/python3.8/site-packages/dirty_equals/_base.py:20: in __eq__
    return self() == other
venv38/lib64/python3.8/site-packages/dirty_equals/_sequence.py:181: in __init__
    length=_length_repr(self.length),
E   NameError: name '_length_repr' is not defined

After some rabbit hole crawling I could zoom in on a minimal reproduction procedure.
The problem started with the release of typing-extensions 4.12.1 (June 1st, 2024) and happens when xarray is imported before dirty_equals.

Example in a fresh docker run --rm -it python:3.8 /bin/bash container:

root@0a0f1606b929:/# pip install xarray dirty-equals typing-extensions
...
Successfully installed dirty-equals-0.7.1.post0 numpy-1.24.4 packaging-24.0 pandas-2.0.3 python-dateutil-2.9.0.post0 pytz-2024.1 six-1.16.0 typing-extensions-4.12.1 tzdata-2024.1 xarray-2023.1.0
...

root@0a0f1606b929:/# python
>>> import xarray
>>> import dirty_equals
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/dirty_equals/__init__.py", line 38, in <module>
    from ._sequence import Contains, HasLen, IsList, IsListOrTuple, IsTuple
  File "/usr/local/lib/python3.8/site-packages/dirty_equals/_sequence.py", line 215, in <module>
    class IsList(IsListOrTuple[List[Any]]):
  File "/usr/local/lib/python3.8/typing.py", line 258, in inner
    return cached(*args, **kwds)
  File "/usr/local/lib/python3.8/typing.py", line 898, in __class_getitem__
    return _GenericAlias(cls, params)
  File "/usr/local/lib/python3.8/typing.py", line 672, in __init__
    self.__parameters__ = _collect_type_vars(params)
  File "/usr/local/lib/python3.8/site-packages/typing_extensions.py", line 2994, in _collect_type_vars
    enforce_default_ordering = _has_generic_or_protocol_as_origin()
  File "/usr/local/lib/python3.8/site-packages/typing_extensions.py", line 2961, in _has_generic_or_protocol_as_origin
    return frame.f_locals.get("origin") in (
  File "/usr/local/lib/python3.8/site-packages/dirty_equals/_base.py", line 20, in __eq__
    return self() == other
  File "/usr/local/lib/python3.8/site-packages/dirty_equals/_sequence.py", line 181, in __init__
    length=_length_repr(self.length),
NameError: name '_length_repr' is not defined

Possible workarounds:

  • pin down typing-extensions<4.12.1
  • use python 3.9 or higher
  • import dirty_equals before xarray
@soxofaan
Copy link
Author

soxofaan commented Jun 6, 2024

To give a bit more info about how the NameError occurs from current https://github.com/samuelcolvin/dirty-equals/blob/2c85486b29492977f82c850eadf1afc0e2a9434e/dirty_equals/_sequence.py:

IsListOrTuple.__init__ at line 87 calls _length_repr which is defined at line 257. Nothing out of the ordrinary.

But this line 215 in between:

class IsList(IsListOrTuple[List[Any]]):

seems to trigger an instantiation of IsListOrTuple on Python 3.8 when xarray is already imported and typing-extensions 4.12.1 is installed. The instantiation doesn't work as _length_repr isn't defined yet causing the NameError

@soxofaan
Copy link
Author

soxofaan commented Jun 6, 2024

This might actually just be a bug/regression in typing-extensions as noted in python/typing_extensions#425

@soxofaan
Copy link
Author

This issue seems to be resolved with recent typing-extensions 4.12.2 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

No branches or pull requests

1 participant