Introduce generic logger type in loggeradapter#5954
Introduce generic logger type in loggeradapter#5954JelleZijlstra merged 2 commits intopython:masterfrom
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. |
Akuli
left a comment
There was a problem hiding this comment.
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]):
passAs |
|
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):
passThings 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, butpyrightreportsWith the proposed changes,
pyrightis able to infer the correct type of wrapped logger instance.