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

[BUG] Unexpected name for attribute "spec_for_test.test_distutils" in class _distutils_hack.DistutilsMetaFinder #3470

Closed
SylvainDe opened this issue Jul 29, 2022 · 3 comments
Labels

Comments

@SylvainDe
Copy link

setuptools version

63.2.0

Python version

Python 3.11.0b5

OS

Linux

Additional environment information

Only on the latest Python beta version (11.b5) - found on Github actions.

Description

Disclaimer: This is probably not an actual bug and highly non-important but something which surprised me a bit and I wanted to be sure it was the expected behavior.

I wanted to match a regexp matching the names of attributes in Python(1) and I decided to use introspection to test it: check every attribute name (using "dir") for every classes I could find.

Starting on Python 3.11.0b5, I caught something I did not expect: the _distutils_hack.DistutilsMetaFinder' has an attribute named 'spec_for_test.test_distutils' which is pretty peculiar. This behavior did not exist on Python 3.11.0b4.

(1) I am aware that attribute name can be pretty much anything when someone relies on setattr.

Expected behavior

I would expect most attribute names to match the following regexp: '^[^\\d\\W]\w*$' .

How to Reproduce

None

Output

None

@SylvainDe SylvainDe added bug Needs Triage Issues that need to be evaluated for severity and status. labels Jul 29, 2022
@abravalheri
Copy link
Contributor

Hi @SylvainDe , _distutils_hack relies on both setattr and getattr, so unless the interpreter spontaneously throws an error, I would not be concerned...

You mentioned that in Python 3.11.0b5 you see an error. Is this error something your code is throwing, or something that just happens without any intervention?

BTW, in other contexts you can also use the str.isidentifier function instead of creating a regex.

@SylvainDe
Copy link
Author

Hello @abravalheri , thanks for your feedback! I was just surprised by the usage of such an attribute name but it does not cause any exception to be thrown anywhere by the interpreter. The only error comes from a wrong assumption in my code parsing attribute name.

As for the str.isidentifier function, this is an excellent suggestion but my use case is slightly more tricky: I actually try to parse exception messages such as AttributeError and as such, I include the regexp for attribute name in another bigger regexp.

If having an unusual name is fine for you, it is fine for me as well and the issue can be closed.

@jaraco
Copy link
Member

jaraco commented Aug 3, 2022

The reason why these attributes exist is more apparent when looking at the implementation. DistutilsMetaFinder uses methods named spec_for_{name} to handle a module indicated by {name}. In order to provide special handling for tests.test_distutils, the DistutilsMetaFinder has a method spec_for_test.test_distutils (motivation in #3383). Yes, that method is abnormal, and yes, that method would be unreachable through the normal Python accessor syntax, but it works for its purposes, and it should go away when Python removes test.test_distutils.

This behavior did not exist on Python 3.11.0b4.

I suspect this change is due to a later version of Setuptools being bundled with Python, or possibly just a later version of Setuptools being loaded in your environment.

Thanks for reporting, but I believe this is working as intended (despite the smell).

@jaraco jaraco closed this as completed Aug 3, 2022
@jaraco jaraco added question and removed bug Needs Triage Issues that need to be evaluated for severity and status. labels Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants