gh-118465: Add __firstlineno__ attribute to class#118475
gh-118465: Add __firstlineno__ attribute to class#118475serhiy-storchaka merged 7 commits intopython:mainfrom
Conversation
It is set by compiler with the line number of the first line of the class definition.
|
|
||
| * Classes have a new :attr:`!__firstlineno__` attribute, | ||
| populated by the compiler, with the line number of the first line | ||
| of the class definition. |
There was a problem hiding this comment.
If the class has decorators then I think this is the first line of the first decorator. Need a test for this case, and probably to mention in the doc.
There was a problem hiding this comment.
There are existing tests, and they are passed.
I'll add explicit mentioning of this fact, but AFAIK it was not specified for co_firstlineno etc.
Co-authored-by: Mark Shannon <mark@hotpy.org>
|
|
||
| * Classes have a new :attr:`!__firstlineno__` attribute, | ||
| populated by the compiler, with the line number of the first line | ||
| of the class definition. |
There was a problem hiding this comment.
There are existing tests, and they are passed.
I'll add explicit mentioning of this fact, but AFAIK it was not specified for co_firstlineno etc.
|
It is possible to construct classes without def f():
__firstlineno__ = 1
class X:
nonlocal __firstlineno__
return X
print(f().__firstlineno__) # AttributeErrorOr: class Y:
global __firstlineno__
print(Y.__firstlineno__) # AttributeErrorThis is obviously an extreme edge case though. You could also do this by manipulating Is it worth it to make |
|
Good point. I thought that it is the same as for We can try to set I think that for backward compatibility |
It is set by compiler with the line number of the first line of the class definition.
📚 Documentation preview 📚: https://cpython-previews--118475.org.readthedocs.build/