GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-118465: Add __firstlineno__ attribute to class by serhiy-storchaka · Pull Request #118475 · python/cpython

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented May 1, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

It is set by compiler with the line number of the first line of the class definition.

  • Issue: pydoc is obscenely slow for some modules #118465

📚 Documentation preview 📚: https://cpython-previews--118475.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
pythongh-118465: Add __firstlineno__ attribute to class
2d78936
It is set by compiler with the line number of the first line of
the class definition.
@serhiy-storchaka
serhiy-storchaka requested review from AlexWaygood, JelleZijlstra, ethanfurman, iritkatriel and markshannon as code owners May 1, 2024 14:19
@bedevere-app bedevere-app Bot mentioned this pull request May 1, 2024
pydoc is obscenely slow for some modules #118465
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label May 1, 2024
markshannon
markshannon reviewed May 1, 2024
View reviewed changes
Comment thread Doc/reference/datamodel.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

iritkatriel
iritkatriel reviewed May 1, 2024
View reviewed changes
Comment thread Doc/whatsnew/3.13.rst

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

@iritkatriel iritkatriel May 1, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka May 2, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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 for co_firstlineno etc.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka @markshannon
Update Doc/reference/datamodel.rst
2ac81ad
Co-authored-by: Mark Shannon <[email protected]>
serhiy-storchaka
serhiy-storchaka commented May 2, 2024
View reviewed changes
Comment thread Doc/whatsnew/3.13.rst

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

@serhiy-storchaka serhiy-storchaka May 2, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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 for co_firstlineno etc.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/reference/datamodel.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added 2 commits May 2, 2024 17:10
@serhiy-storchaka
Update Doc/reference/datamodel.rst
bc318a0
@serhiy-storchaka
Merge branch 'main' into class-firstlineno
c168577
@JelleZijlstra

JelleZijlstra commented May 4, 2024

Copy link
Copy Markdown
Member

It is possible to construct classes without __firstlineno__:

def f():
    __firstlineno__ = 1
    class X:
        nonlocal __firstlineno__
    return X
print(f().__firstlineno__)  # AttributeError

Or:

class Y:
    global __firstlineno__
print(Y.__firstlineno__)  # AttributeError

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

Is it worth it to make inspect 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__'
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added 3 commits May 5, 2024 09:32
@serhiy-storchaka
Merge branch 'main' into class-firstlineno
e557f02
@serhiy-storchaka
Handle classes without __firstlineno__.
3edda57
@serhiy-storchaka
Bump the pyc magic number.
3939135
@serhiy-storchaka
serhiy-storchaka requested review from brettcannon, ericsnowcurrently, ncoghlan and warsaw as code owners May 5, 2024 07:02
@serhiy-storchaka

serhiy-storchaka commented May 5, 2024

Copy link
Copy Markdown
Member Author

Good point. I thought that it is the same as for __module__ and __qualname__, but it turned out that they are set implicitly in the type constructor even when not specified in the class dict. They are set even for type('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 the type() call rather than in the class statement does not help in the only use case for this attribute.

I think that for backward compatibility getsource() should convert an AttributeError into OSError which was raised before for classes created by the type() call.

AlexWaygood and JelleZijlstra reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra
JelleZijlstra approved these changes May 5, 2024
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 5, 2024
@serhiy-storchaka
serhiy-storchaka merged commit 153b3f7 into python:main May 6, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label May 6, 2024
@serhiy-storchaka
serhiy-storchaka deleted the class-firstlineno branch May 6, 2024 09:02
SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
@serhiy-storchaka @SonicField
pythongh-118465: Add __firstlineno__ attribute to class (pythonGH-118475
7670241
)

It is set by compiler with the line number of the first line of
the class definition.
@frenzymadness frenzymadness mentioned this pull request May 13, 2024
Test failure with Python 3.13.0b1 cloudpipe/cloudpickle#533
Closed
@devdanzin devdanzin mentioned this pull request Aug 14, 2024
3.13 Regression: inspect.getsource() returns incorrect source code #122981
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iritkatriel iritkatriel iritkatriel left review comments
@markshannon markshannon markshannon left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@ethanfurman ethanfurman Awaiting requested review from ethanfurman ethanfurman is a code owner
@AlexWaygood AlexWaygood Awaiting requested review from AlexWaygood AlexWaygood is a code owner
@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@serhiy-storchaka @JelleZijlstra @iritkatriel @markshannon
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.