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

Use threading.RLock for logging.Handler.lock #13390

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

InFiNiTy0639
Copy link

Fixing Type Annotation for lock in Handler class

The type annotation for the lock attribute in the Handler class was previously incorrectly defined as threading.Lock | None in the typeshed stub. This is inconsistent with the CPython implementation, which uses threading.RLock (a reentrant lock) instead of a simple lock.

Changes made:

  • Updated the type annotation for the lock attribute in the Handler class from threading.Lock | None to threading.RLock | None.
  • This change aligns the typeshed stub with the actual implementation in CPython, where RLock is used instead of Lock.

Reasoning:

  • The CPython source code (from version 2.5 onward) uses threading.RLock instead of threading.Lock for the lock attribute in the Handler class.
  • RLock is not a subclass of Lock, so using Lock in the typeshed definition was incorrect.
  • This update ensures the correct typing is reflected and avoids confusion when using static analysis tools or type checkers.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@@ -246,7 +246,7 @@ NOTSET: Final = 0
class Handler(Filterer):
level: int # undocumented
formatter: Formatter | None # undocumented
lock: threading.Lock | None # undocumented
lock: threading.RLock | None # Corrected type
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the comment needs to be changed.

Copy link
Author

Choose a reason for hiding this comment

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

Ok 👍🙂

@AlexWaygood AlexWaygood changed the title Fixes issus #13382 Use threading.RLock for logging.Handler.lock Jan 11, 2025
@AlexWaygood AlexWaygood linked an issue Jan 11, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

logging.Handler.lock should actually be threading.RLock
2 participants