You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In contrast, on 3.12 and below, this raises an OSError (not ideal but also not wrong):
$ docker run --rm python:3.12-rc python -c "import inspect, ssl; print(inspect.getsource(ssl.AlertDescription))"
Traceback (most recent call last):
File "<string>", line 1, in<module>
File "/usr/local/lib/python3.12/inspect.py", line 1285, in getsource
lines, lnum = getsourcelines(object)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/inspect.py", line 1267, in getsourcelines
lines, lnum = findsource(object)
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/inspect.py", line 1112, in findsource
raise OSError('could not find class definition')
OSError: could not find class definition
It happens because class ssl.AlertDescription now has a __firstlineno__ attribute, and that is erroneously interpreted as a line number in ssl.py. It shares the same line number (1334) with other classes, like SSLErrorNumber, created via IntEnum._convert_. This line number corresponds to IntEnum's line number in enum.py instead.
So ISTM the issue is that the construction of __firstlineno__ for these classes is capturing the line number of the class generator definition instead of the class creation site.
Thank you for your report @mhils. I agree with @devdanzin analysis. __firstlineno__ is not set for these enum classes, but inherited from the base class. There is a test for generated class, but it does not have a base class that has __firstlineno__.
Thank you two again! I can confirm that the issue is fixed now for ssl.AlertDescription with 3.13rc2. Unfortunately our smoke tests still show a very similar error in this case here:
Activity
devdanzin commentedon Aug 14, 2024
Bisected to 153b3f7, introduced by #118475.
It happens because class
ssl.AlertDescriptionnow has a__firstlineno__attribute, and that is erroneously interpreted as a line number in ssl.py. It shares the same line number (1334) with other classes, likeSSLErrorNumber, created viaIntEnum._convert_. This line number corresponds toIntEnum's line number in enum.py instead.So ISTM the issue is that the construction of
__firstlineno__for these classes is capturing the line number of the class generator definition instead of the class creation site.cc @serhiy-storchaka
pythongh-122981: Fix inspect.getsource() for generated classes with P…
serhiy-storchaka commentedon Aug 14, 2024
Thank you for your report @mhils. I agree with @devdanzin analysis.
__firstlineno__is not set for these enum classes, but inherited from the base class. There is a test for generated class, but it does not have a base class that has__firstlineno__.#123001 should fix this.
pythongh-122981: Fix inspect.getsource() for generated classes with P…
mhils commentedon Aug 14, 2024
Awesome - thank you two for getting to the bottom of this with lightning speed! ⚡🍰 😃
gh-122981: Fix inspect.getsource() for generated classes with Python …
pythongh-122981: Fix inspect.getsource() for generated classes with P…
pythongh-122981: Fix inspect.getsource() for generated classes with P…
[3.13] gh-122981: Fix inspect.getsource() for generated classes with …
mhils commentedon Sep 10, 2024
Thank you two again! I can confirm that the issue is fixed now for
ssl.AlertDescriptionwith 3.13rc2. Unfortunately our smoke tests still show a very similar error in this case here:$ docker run --rm python:3.13.0rc2 bash -c \ 'pip install pluggy==1.5.0; python -c "import inspect, pluggy; print(inspect.getsource(pluggy.PluggyTeardownRaisedWarning))"' [...] "PluginManager",docker run --rm python:3.12 bash -c \ 'pip install pluggy==1.5.0; python -c "import inspect, pluggy; print(inspect.getsource(pluggy.PluggyTeardownRaisedWarning))"' [...] OSError: could not find class definition(https://github.com/mitmproxy/pdoc/actions/runs/10772390037/job/29935976461?pr=730)
Would you like me to file a new issue, or do we keep it here?
Thanks! 😃
serhiy-storchaka commentedon Sep 11, 2024
pluggy.PluggyTeardownRaisedWarningis not the part of the stdlib, so I cannot say what exactly happened, but this may be related to #123339.