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

gh-123339: Fix cases of inconsistency of __module__ and __firstlineno__ in classes #123613

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Sep 2, 2024

  • Setting the __module__ attribute for a class now removes the __firstlineno__ item from the type's dict.
  • The _collections_abc and _pydecimal modules now completely replace the collections.abc and decimal modules after importing the latter. This allows to get the source of classes and functions defined in these modules.
  • inspect.findsource() now checks whether the first line number for a class is out of bound.

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.13 bugs and security fixes label Sep 2, 2024
@bedevere-app bedevere-app bot added the docs Documentation in the Doc dir label Sep 2, 2024
@serhiy-storchaka serhiy-storchaka requested review from a team and rhettinger September 2, 2024 18:09
@Eclips4 Eclips4 changed the title __ in classesgh-123339: Fix cases of inconsistency of __module__ and __firstlineno__ in classes gh-123339: Fix cases of inconsistency of __module__ and __firstlineno__ in classes Sep 2, 2024
@Eclips4
Copy link
Member

Eclips4 commented Sep 2, 2024

Serhiy, it's seems that you forgot to commit the actual fix(and tests) and accidentally added two NEWS entries

…lineno__ in classes

* Setting the __module__ attribute for a class now removes the
  __firstlineno__ item from the type's dict.
* The _collections_abc and _pydecimal modules now completely replace the
  collections.abc and decimal modules after importing the latters. This
  allows to get the source of classes and functions defined in these
  modules.
* inspect.findsource() now checks whether the first line number for a
  class is out of bound.
@serhiy-storchaka
Copy link
Member Author

Thanks for noticing this @Eclips4! I somewhere accidentally used --amend.

Two NEWS entries are intentional, as they belong to different sections (Core and Library).

Lib/inspect.py Show resolved Hide resolved
@@ -1008,6 +1008,7 @@ Special attributes:

:attr:`__firstlineno__`
The line number of the first line of the class definition, including decorators.
Setting :attr:`__module__` removes :attr:`__firstlineno__`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Setting :attr:`__module__` removes :attr:`__firstlineno__`.
Setting :attr:`!__module__` removes :attr:`!__firstlineno__`.

Note that the link to __module__ does not exist (we don't have a global .. attribute:: __module__). We can address this issue in a follow-up PR, but for now, let's just suppress the link.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, the link to __firstlineno__ should be suppressed, but the link to __module__ may work in future.

Lib/collections/abc.py Show resolved Hide resolved
Lib/inspect.py Show resolved Hide resolved
@@ -1008,6 +1008,7 @@ Special attributes:

:attr:`__firstlineno__`
The line number of the first line of the class definition, including decorators.
Setting :attr:`__module__` removes :attr:`__firstlineno__`.
Copy link
Member Author

Choose a reason for hiding this comment

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

Well, the link to __firstlineno__ should be suppressed, but the link to __module__ may work in future.

Doc/reference/datamodel.rst Outdated Show resolved Hide resolved
@rhettinger rhettinger removed their request for review September 3, 2024 10:24
@serhiy-storchaka serhiy-storchaka merged commit 69a4063 into python:main Sep 28, 2024
37 checks passed
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the firstlineno-with-changed-module branch September 28, 2024 17:51
@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 69a4063ca516360b5eb96f5432ad9f9dfc32a72e 3.13

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Sep 28, 2024
…__firstlineno__ in classes (pythonGH-123613)

* Setting the __module__ attribute for a class now removes the
  __firstlineno__ item from the type's dict.
* The _collections_abc and _pydecimal modules now completely replace the
  collections.abc and decimal modules after importing them. This
  allows to get the source of classes and functions defined in these
  modules.
* inspect.findsource() now checks whether the first line number for a
  class is out of bound.
(cherry picked from commit 69a4063)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Sep 28, 2024

GH-124735 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Sep 28, 2024
@serhiy-storchaka serhiy-storchaka removed their assignment Sep 28, 2024
Yhg1s pushed a commit that referenced this pull request Sep 30, 2024
…tlineno__ in classes (GH-123613) (#124735)

* Setting the __module__ attribute for a class now removes the
  __firstlineno__ item from the type's dict.
* The _collections_abc and _pydecimal modules now completely replace the
  collections.abc and decimal modules after importing them. This
  allows to get the source of classes and functions defined in these
  modules.
* inspect.findsource() now checks whether the first line number for a
  class is out of bound.
(cherry picked from commit 69a4063)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants