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

AttributeError __spec__ on Python 3.11 #273

Closed
pawamoy opened this issue Sep 13, 2021 · 18 comments
Closed

AttributeError __spec__ on Python 3.11 #273

pawamoy opened this issue Sep 13, 2021 · 18 comments

Comments

@pawamoy
Copy link

pawamoy commented Sep 13, 2021

Running pytest on Python 3.11:

% python3.11 -m pytest -c config/pytest.ini -n auto tests
Traceback (most recent call last):
  File "/home/pawamoy/.cache/pdm/packages/py-1.10.0-py2.py3-none-any/lib/py/_vendored_packages/apipkg/__init__.py", line 145, in __makeattr
    modpath, attrname = self.__map__[name]
                        ~~~~~~~~~~~~^^^^^^
KeyError: '__spec__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pawamoy/.basher-packages/pyenv/pyenv/versions/3.11-dev/lib/python3.11/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.basher-packages/pyenv/pyenv/versions/3.11-dev/lib/python3.11/runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.basher-packages/pyenv/pyenv/versions/3.11-dev/lib/python3.11/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
    ^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/pytest-6.2.5-py3-none-any/lib/pytest/__init__.py", line 5, in <module>
    from _pytest.assertion import register_assert_rewrite
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/pytest-6.2.5-py3-none-any/lib/_pytest/assertion/__init__.py", line 9, in <module>
    from _pytest.assertion import rewrite
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/pytest-6.2.5-py3-none-any/lib/_pytest/assertion/rewrite.py", line 34, in <module>
    from _pytest.assertion import util
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/pytest-6.2.5-py3-none-any/lib/_pytest/assertion/util.py", line 13, in <module>
    import _pytest._code
    ^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/pytest-6.2.5-py3-none-any/lib/_pytest/_code/__init__.py", line 2, in <module>
    from .code import Code
    ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/pytest-6.2.5-py3-none-any/lib/_pytest/_code/code.py", line 54, in <module>
    class Code:
    ^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/pytest-6.2.5-py3-none-any/lib/_pytest/_code/code.py", line 81, in Code
    def path(self) -> Union[py.path.local, str]:
                            ^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/py-1.10.0-py2.py3-none-any/lib/py/_vendored_packages/apipkg/__init__.py", line 152, in __makeattr
    result = importobj(modpath, attrname)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pawamoy/.cache/pdm/packages/py-1.10.0-py2.py3-none-any/lib/py/_vendored_packages/apipkg/__init__.py", line 72, in importobj
    module = __import__(modpath, None, None, ['__doc__'])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1044, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
  File "/home/pawamoy/.cache/pdm/packages/py-1.10.0-py2.py3-none-any/lib/py/_vendored_packages/apipkg/__init__.py", line 150, in __makeattr
    raise AttributeError(name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: __spec__

Note: I'm using PDM which uses .pth files in __pypackages__ that point to actual installed packages in ~/.cache/pdm/packages (it allows dedup of packages across projects, saving space and installation time). Not sure this has to do with the issue, but I hear py/apipkg do some black magic on themselves, so this might conflict. It works well on other Python versions though (3.8, 3.9 and 3.10).

@RonnyPfannschmidt
Copy link
Member

This may require a coordinated release of py and apipkg, im not sure if I have the near term capacity

If the pending apipkg 2 can work, ill haste the release

Else i need to find time/motivation to fix apipkg for python 3.11

@pawamoy
Copy link
Author

pawamoy commented Sep 14, 2021

Well there's no rush of course, as Python 3.11 is not gonna be released soon 🙂
If you can give directions on how to tackle this, maybe someone else will be willing to do it!

@RonnyPfannschmidt
Copy link
Member

First a verification of standalone apipkg is needed, then after fix it has to be included in the vendored items

@greyblue9
Copy link

The cause of this error I tracked down to this change (commit 0a8ae8a50a0fea3d39ec49b220a5c7a5b70e36f8 in cpython):
'bpo-44717: improve AttributeError on circular imports of submodules (GH-27338)'
https://bugs.python.org/issue44717

it introduces the parent = parent_module.__spec__ line in importlib.

AttributeError: __spec__ (which is thrown from importlib, while KeyError in apipkg is being handled)
The latest master and modernization branches fix the issue in pytest (and apipkg standalone) for me.

From the stack in importlib:

File /data/media/0/src/python3/src/Lib/importlib/_bootstrap.py, line 1044, in _find_and_load
    return _find_and_load_unlocked(name, import_):
  File /data/media/0/src/python3/src/Lib/importlib/_bootstrap.py, line 994, in _find_and_load_unlocked
    _call_with_frames_removed(import_, parent):
  File /data/media/0/src/python3/src/Lib/importlib/_bootstrap.py, line 241, in _call_with_frames_removed
    return f(*args, **kwds):
  File /data/media/0/src/python3/src/Lib/importlib/_bootstrap.py, line 1044, in _find_and_load
    return _find_and_load_unlocked(name, import_):
  File /data/media/0/src/python3/src/Lib/importlib/_bootstrap.py, line 1004, in _find_and_load_unlocked
    parent_spec = parent_module.__spec__:

@RonnyPfannschmidt
Copy link
Member

@greyblue9 thansk for the update, so i will update the vendored copy next

@RonnyPfannschmidt
Copy link
Member

@greyblue9 i finished the upgrade of the vendored copy, it be neat to get a quick verify just in case so we can prepare a release

@pawamoy
Copy link
Author

pawamoy commented Sep 24, 2021

I copied the contents of https://github.com/pytest-dev/apipkg/blob/main/src/apipkg/__init__.py into ~/.cache/pdm/packages/py-1.10.0-py2.py3-none-any/lib/py/_vendored_packages/apipkg/__init__.py (this is where PDM installs the py package), ran tests again on Python 3.11, and they passed 👍

@RonnyPfannschmidt
Copy link
Member

@pawamoy awesome, thanks

@hroncok
Copy link
Member

hroncok commented Oct 11, 2021

Now when Python 3.11.0a1 is out, we (Fedora) got hit by this on our CI.

@greyblue9
Copy link

@greyblue9 i finished the upgrade of the vendored copy, it be neat to get a quick verify just in case so we can prepare a release

Sorry, I didn't see your comment until just now. What should I do to verify?

@RonnyPfannschmidt
Copy link
Member

@greyblue9 no longer needed, initial verification already happened, we need to release now

@greyblue9
Copy link

@greyblue9 no longer needed, initial verification already happened, we need to release now

thanks @RonnyPfannschmidt , let me know if I can be of any further help

@hroncok
Copy link
Member

hroncok commented Nov 4, 2021

@RonnyPfannschmidt Is there anything we can do to expedite the release? We are reporting Python 3.11 test failures to various other open source projects and broken pytest makes it harder to reproduce them for their maintainers. Re-installing py from the master branch works. Let me know if you need to check this py change on all Fedora packages to see if it doesn't break anything else, or if there is anything else we can do. Thanks ;)

@RonnyPfannschmidt
Copy link
Member

@hroncok thanks for the ping, i completely forgot that #277 was already preparing that over my cold
im preparing to push a tag once hugos pr passes with the updated date

@RonnyPfannschmidt
Copy link
Member

@hroncok release is out, happy testing

@hroncok
Copy link
Member

hroncok commented Nov 4, 2021

@RonnyPfannschmidt
Copy link
Member

closing as released

webkit-early-warning-system pushed a commit to WebKit/WebKit that referenced this issue Dec 20, 2022
https://bugs.webkit.org/show_bug.cgi?id=249564

Reviewed by Jonathan Bedard.

test-webkitpy was failing to run with Python 3.11. AttributeError
exception was thrown in apipkg.py of py. The latest py 1.11.0 fixed
the problem.
<pytest-dev/py#273>

Upgraded all pytest related modules to the latest.
py 1.11.0 supports Python 2.7 and >3.5.
pytest 7.2.0 and pytest-asyncio 0.20.3 support Python >3.7.
pytest-timeout 2.1.0 supports Python >3.6.

* Tools/Scripts/webkitpy/__init__.py:

Canonical link: https://commits.webkit.org/258166@main
@namedgraph

This comment was marked as off-topic.

linsword13 added a commit to linsword13/ramble that referenced this issue May 31, 2024
* Remove relevant usage of `six.moves` in ramble and vendored deps
* Update `py` dep to the latest [head](https://github.com/pytest-dev/py/tree/6b219734fcd8d2f6489c2f50585a435b34c029c2/py), due to pytest-dev/py#273.
linsword13 added a commit to linsword13/ramble that referenced this issue May 31, 2024
* Remove relevant usage of `six.moves` in ramble and vendored deps
* Update `py` dep to the latest [head](https://github.com/pytest-dev/py/tree/6b219734fcd8d2f6489c2f50585a435b34c029c2/py), due to pytest-dev/py#273.
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

5 participants