-
-
Notifications
You must be signed in to change notification settings - Fork 2.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
**kwargs & relations between int, float & bool #9182
Comments
AFAIK casting is the correct way to extract one of many If #4441 was implemented, then you could do something like this: class OptionsDict(TypedDict):
foo: float
bar: int
baz: bool
class MyClass:
def func2(self, *args, str, **kwargs: Expand[OptionsDict]) -> None:
self.bar = kwargs['bar']
# self.bar is now bool, without a type comment or cast |
Hmm, this might work too: def func2(self, *args: str, **kwargs: Union[int, float]) -> None:
self.bar = kwargs['bar']
assert isinstance(self.bar, int)
# what do mypy and your ide say now? |
Hi @Akuli, from typing import Union
class MyClass:
def func2(self, *args: str, **kwargs: Union[int, float]) -> None:
self.bar = kwargs['bar']
assert isinstance(self.bar, int) There are no errors from mypy and the IDE considers self.bar to be an int now. However, with this code .. from typing import Union
class MyClass:
def func2(self, *args: str, **kwargs: Union[int, float]) -> None:
self.bar = kwargs['bar']
assert isinstance(self.bar, str) .. there are still no errors from |
I don't know why mypy doesn't complain if you do |
The original behavior seems reasonable, since |
Hello,
this is a user question.
Python is 3.6.9 and mypy is mypy 0.790+dev.28829fbb684d7535680934b401c05698db5b4d85.
I have a class with methods as below:
In all the functions, **kwargs is a dictionary of str keys with values that may be of several types.
The errors I am getting are:
I would like to clarify if this occurs in relation to this comment in #1850 and the underlying implementation's assumptions of which type is a sub-type of which one?
I can deal with it by using cast, as in the code below, so this is more of a question if the above is expected.
Please note that the type comments are still needed because otherwise my IDE will not understand the results of casting.
Thanks.
The text was updated successfully, but these errors were encountered: