-
-
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
logging.StreamHandler.stream is sys.stderr by default ; "SupportsWrite['str']" is too narrow #5680
Comments
This is a tradeoff. I was deliberating whether to make |
I agree that this is a good use case for an invariant TypeVar, since this is the same situation as with lists. For example, |
Yes. In theory if someone would do the following, type checkers would warn: h = StreamHandler()
h.setStream(StringIO()) But I think that's a good thing that users need to be explicit about this: h: StreamHandler[MyProto] = StreamHandler()
h.setStream(StringIO()) And the following is a bad idea anyway: h = StreamHandler(open("abc", "w"))
h.setStream(StringIO())
h.stream.getvalue() |
@srittau How does typehint the class now with this change?
This only works if the annotation is not evaluated 🤔 |
Indeed. You have to either use |
Sometimes you cannot do that, can we revert this in the meantime? 🤔 E.g. now impossible to write:
🤔 you need to add a type ignore to the inheritance line otherwise mypy will complain that is missing a generic parameter. |
The ultimate solution is to have defaults for generics (python/typing#307), but for now we unfortunately require |
You could also submit a PR to CPython to add |
That wouldn't work for the older Python version though only the latest 3.11 or later 🤔 |
A bit annoying, but maybe better than making a pull request to CPython or using
|
In case it's useful for future reference, this section of the mypy documentation covers ways to work around this class of issue: https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime |
In cases others did not notice it, the |
I've opened python/cpython#92129 to add (Update: merged!) |
With this change, how would one type hint
Because of the invariant generic, it seems that the only valid type for |
This prevents code like
logging.StreamHandler().stream.close()
from being type checked as validhttps://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler
This problem was introduced in https://github.com/python/typeshed/pull/5069/files#diff-23ef9e6197f6f95428d7333190fc2357c1c4f110b89c27a7fc71e2fab7d7897aR702
The text was updated successfully, but these errors were encountered: