8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
The reason for this PR:
import logging logger = logging.getLogger() adapter = logging.LoggerAdapter(logger, dict()) reveal_type(adapter.logger) adapter.logger.getChild("fizz")
has no issues with mypy, but pyright reports
mypy
pyright
/tmp/main.py:5:13 - info: Type of "adapter.logger" is "Logger | LoggerAdapter" /tmp/main.py:6:16 - error: Cannot access member "getChild" for type "LoggerAdapter" Member "getChild" is unknown (reportGeneralTypeIssues)
With the proposed changes, pyright is able to infer the correct type of wrapped logger instance.
Sorry, something went wrong.
introduce generic logger type in loggingadapter
1e7e2c9
Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
work around missing support for recursive typevars
316fa02
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.
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].
logging.LoggerAdapter
logging.LoggerAdapter[logging.Logger]
a74624d
@Akuli @srittau is it an issue that one has to distinct between type checking and runtime now? I mean, LoggerAdapter is not subscriptable, so the type has to be either quoted
LoggerAdapter
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 from __future__ import annotations if possible or quoting of not possible.
from __future__ import annotations
@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 LoggerAdapter is not subscriptable, this class will fail to initialise
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
__class_getitem__
logging.StreamHandler
I've opened python/cpython#92129 to add __class_getitem__ to the class in 3.11+.
(Update: merged!)
srittau srittau approved these changes
Akuli Akuli approved these changes
Successfully merging this pull request may close these issues.