You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice if Mypy was able to resolve the types in the below snippet:
importosimporttypingastT=t.TypeVar('T')
R=t.TypeVar('R')
classflatmap(t.Generic[T, R]):
def__init__(self, func: t.Callable[[T], R]) ->None:
self.func=funcdef__ror__(self, value: t.Optional[T]) ->t.Optional[R]:
ifvalueisnotNone:
returnself.func(value)
returnNone# error: Cannot infer type argument 1 of "flatmap"# error: "object" has no attribute "upper"print(os.getenv('VARNAME') |flatmap(lambdam: m.upper()))
print(os.getenv('VARNAME') |flatmap(str.upper)) # ok
Pitch
Looking at just flatmap(lambda m: m.upper()), neither T nor R can be immediately determined.
However, it should be possible to deduce the type variables by taking into account the broader
context.
T = str can be derived from the __ror__() call
After propagating T into the flatmap() and lambda, R = str can be deduced from the lambda body
One potential use case is already highlighted in the example above. Being able to chain the flatmap() function using ror adds a lot of readability compared to a normal call:
The obvious workaround in this example would be to use str.upper which would satisfy Mypy because the function is already typed. However, there are other example where the there is no already-typed alternative, like accessing an attribute of an optional object. Nr. 1 would be preferred over 2 for it's better readability. 3 is an alternative without a helper function, though that also gets harder to read as you start to work with more optional values.
This would be nice to support, but it would require a major redesign of the type inference engine used in mypy. Currently mypy performs type inference essentially one expression at a time, and here we'd need to build constraints for multiple expressions and solve them together.
Feature
It would be nice if Mypy was able to resolve the types in the below snippet:
Pitch
Looking at just
flatmap(lambda m: m.upper())
, neitherT
norR
can be immediately determined.However, it should be possible to deduce the type variables by taking into account the broader
context.
T = str
can be derived from the__ror__()
callT
into theflatmap()
andlambda
,R = str
can be deduced from the lambda bodyOne potential use case is already highlighted in the example above. Being able to chain the
flatmap()
function usingror
adds a lot of readability compared to a normal call:The obvious workaround in this example would be to use
str.upper
which would satisfy Mypy because the function is already typed. However, there are other example where the there is no already-typed alternative, like accessing an attribute of an optional object. Nr. 1 would be preferred over 2 for it's better readability. 3 is an alternative without a helper function, though that also gets harder to read as you start to work with more optional values.The text was updated successfully, but these errors were encountered: