-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Introduce generic logger type in loggeradapter #5954
Conversation
Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
This comment has been minimized.
This comment has been minimized.
Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
Diff from mypy_primer, showing the effect of this PR on open source code: sphinx (https://github.com/sphinx-doc/sphinx.git)
- sphinx/util/logging.py: note: In member "handle" of class "SphinxLoggerAdapter":
- sphinx/util/logging.py:140:9: error: Item "LoggerAdapter" of "Union[Logger, LoggerAdapter]" has no attribute "handle"
porcupine (https://github.com/Akuli/porcupine.git)
+ porcupine/plugins/langserver.py:129: error: Missing type parameters for generic type "LoggerAdapter" [type-arg]
+ porcupine/plugins/langserver.py:143: error: Missing type parameters for generic type "LoggerAdapter" [type-arg]
+ porcupine/plugins/langserver.py:323: error: Missing type parameters for generic type "LoggerAdapter" [type-arg]
+ porcupine/plugins/langserver.py:724: error: Missing type parameters for generic type "LoggerAdapter" [type-arg]
|
Cc @Akuli as someone affected by this change. |
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.
I have some functions that take an argument of type logging.LoggerAdapter
, and I just have to change those to logging.LoggerAdapter[logging.Logger]
.
@Akuli @srittau is it an issue that one has to distinct between type checking and runtime now? I mean, x: "logging.LoggerAdapter[logging.Logger]" = ... or aliased via e.g. if TYPE_CHECKING:
import logging
LoggerAdapter = logging.LoggerAdapter[logging.Logger]
else:
from logging import LoggerAdapter
x: LoggerAdapter = ... |
That's a general issue, but temporary. I would recommend to use |
@srittau @hoefling What would be the recommendation for inheritance? From what I understand importing annotations or quoting would not be as useful here import logging
class Adapter(logging.LoggerAdapter[logging.Logger]):
pass As |
This is a common problem, and unfortunately it's annoying to deal with. This works, but it's messy and feels like a hack: import logging
from typing import TYPE_CHECKING
if TYPE_CHECKING:
_LoggerAdapter = logging.LoggerAdapter[logging.Logger]
else:
_LoggerAdapter = logging.LoggerAdapter
class MyAdapter(_LoggerAdapter):
pass Things that would help, but don't exist yet:
|
Linking the relevant section of mypy docs, in case it helps people: https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime |
I've opened python/cpython#92129 to add (Update: merged!) |
The reason for this PR:
has no issues with
mypy
, butpyright
reportsWith the proposed changes,
pyright
is able to infer the correct type of wrapped logger instance.