Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-118465: Add __firstlineno__ attribute to class#118475

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commentedMay 1, 2024
edited by github-actionsbot
Loading

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/

It is set by compiler with the line number of the first line ofthe class definition.
@@ -280,6 +280,11 @@ Other Language Changes
class scopes are not inlined into their parent scope. (Contributed by
Jelle Zijlstra in :gh:`109118` and :gh:`118160`.)

* Classes have a new :attr:`!__firstlineno__` attribute,
populated by the compiler, with the line number of the first line
of the class definition.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

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.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

There are existing tests, and they are passed.

I'll add explicit mentioning of this fact, but AFAIK it was not specified forco_firstlineno etc.

Co-authored-by: Mark Shannon <mark@hotpy.org>
@@ -280,6 +280,11 @@ Other Language Changes
class scopes are not inlined into their parent scope. (Contributed by
Jelle Zijlstra in :gh:`109118` and :gh:`118160`.)

* Classes have a new :attr:`!__firstlineno__` attribute,
populated by the compiler, with the line number of the first line
of the class definition.
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

There are existing tests, and they are passed.

I'll add explicit mentioning of this fact, but AFAIK it was not specified forco_firstlineno etc.

@JelleZijlstra
Copy link
Member

It is possible to construct classes without__firstlineno__:

deff():__firstlineno__=1classX:nonlocal__firstlineno__returnXprint(f().__firstlineno__)# AttributeError

Or:

classY:global__firstlineno__print(Y.__firstlineno__)# AttributeError

This is obviously an extreme edge case though. You could also do this by manipulatinglocals() in the class body or the metaclass.

Is it worth it to makeinspect smarter in the presence of classes without__firstlineno__?

  File "/Users/jelle/py/cpython/insp.py", line 18, in <module>    print(inspect.findsource(X))          ~~~~~~~~~~~~~~~~~~^^^  File "/Users/jelle/py/cpython/Lib/inspect.py", line 1070, in findsource    return lines, object.__firstlineno__ - 1                  ^^^^^^^^^^^^^^^^^^^^^^AttributeError: type object 'X' has no attribute '__firstlineno__'

@serhiy-storchaka
Copy link
MemberAuthor

Good point. I thought that it is the same as for__module__ and__qualname__, but it turned out that they are set implicitly in thetype constructor even when not specified in the class dict. They are set even fortype('A', (), {}).

We can try to set__firstlineno__ to the current line number in the top Python frame, but it is safer to not set it at all. Setting__firstlineno__ for classes created by thetype() call rather than in theclass statement does not help in the only use case for this attribute.

I think that for backward compatibilitygetsource() should convert an AttributeError into OSError which was raised before for classes created by thetype() call.

AlexWaygood and JelleZijlstra reacted with thumbs up emoji

@serhiy-storchakaserhiy-storchaka merged commit153b3f7 intopython:mainMay 6, 2024
@serhiy-storchakaserhiy-storchaka deleted the class-firstlineno branchMay 6, 2024 09:02
SonicField pushed a commit to SonicField/cpython that referenced this pull requestMay 8, 2024
)It is set by compiler with the line number of the first line ofthe class definition.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@iritkatrieliritkatrieliritkatriel left review comments

@markshannonmarkshannonmarkshannon left review comments

@JelleZijlstraJelleZijlstraJelleZijlstra approved these changes

@ethanfurmanethanfurmanAwaiting requested review from ethanfurmanethanfurman is a code owner

@AlexWaygoodAlexWaygoodAwaiting requested review from AlexWaygoodAlexWaygood is a code owner

@brettcannonbrettcannonAwaiting requested review from brettcannonbrettcannon is a code owner

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrentlyericsnowcurrently is a code owner

@ncoghlanncoghlanAwaiting requested review from ncoghlanncoghlan is a code owner

@warsawwarsawAwaiting requested review from warsawwarsaw is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@serhiy-storchaka@JelleZijlstra@iritkatriel@markshannon

[8]ページ先頭

©2009-2025 Movatter.jp