Make logging.StreamHandler generic over stream#5681
Merged
Akuli merged 1 commit intopython:masterfrom Jun 23, 2021
Merged
Conversation
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pip (https://github.com/pypa/pip.git)
+ src/pip/_internal/utils/logging.py:152: error: Missing type parameters for generic type "StreamHandler"
pytest (https://github.com/pytest-dev/pytest.git)
+ src/_pytest/main.py:505: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
- src/_pytest/main.py:505: error: Returning Any from function declared to return "Path" [no-any-return]
- src/_pytest/python.py:411: error: Need type annotation for "dicts" [var-annotated]
- src/_pytest/logging.py:619: error: Argument 1 to "setStream" of "FileHandler" has incompatible type "TextIO"; expected "TextIOWrapper" [arg-type]
+ src/_pytest/logging.py:318: error: Missing type parameters for generic type "StreamHandler" [type-arg]
+ src/_pytest/logging.py:754: error: Missing type parameters for generic type "StreamHandler" [type-arg]
- src/_pytest/cacheprovider.py:143: error: Returning Any from function declared to return "Path" [no-any-return]
- src/_pytest/cacheprovider.py:153: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
|
Collaborator
Author
|
Looking at primer output: A few cases where the type parameter must be added (another case where generic defaults would be useful). The rest don't seem related to this PR. |
Akuli
approved these changes
Jun 23, 2021
DMRobertson
pushed a commit
to matrix-org/sydent
that referenced
this pull request
Jan 5, 2022
Since python/typeshed#5681 (vendored in recent mypy releases), StreamHandler is now generic over its stream. This causes us pain: > Missing type parameters for generic type "StreamHandler" And I couldn't find a satisfactory type parameter that worked here. - a `TimedRotatingFileHandler` instance is a `FileHandler` which is a `StreamHandler[TextIOWrapper]`. - the instance `StreamHandler()` (which writes to stdout) is overloaded to be a `StreamHandler[TextIO]` `handler: StreamHandler[TextIO]` didn't work. I don't fully understand the difference between the concrete TextIOWrapper and the abstract TextIO (the former looks to be compatible with the latter?). I think the StreamHandler type parameter would need to be covariant for this to work. In any case, we're not making use of any of the stream or file specific attributes here. So let's just mark it as a general `Handler`.
DMRobertson
pushed a commit
to matrix-org/sydent
that referenced
this pull request
Jan 5, 2022
* Remove redunant casts A version of mypy has been released which includes python/typeshed#6150. (Typeshed is vendored with mypy these days.) We can now remove the redundant cast. * Mark logging handler as a generic logging handler. Since python/typeshed#5681 (vendored in recent mypy releases), StreamHandler is now generic over its stream. This causes us pain: > Missing type parameters for generic type "StreamHandler" And I couldn't find a satisfactory type parameter that worked here. - a `TimedRotatingFileHandler` instance is a `FileHandler` which is a `StreamHandler[TextIOWrapper]`. - the instance `StreamHandler()` (which writes to stdout) is overloaded to be a `StreamHandler[TextIO]` `handler: StreamHandler[TextIO]` didn't work. I don't fully understand the difference between the concrete TextIOWrapper and the abstract TextIO (the former looks to be compatible with the latter?). I think the StreamHandler type parameter would need to be covariant for this to work. In any case, we're not making use of any of the stream or file specific attributes here. So let's just mark it as a general `Handler`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #5680