-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Cannot return concrete type within isinstance
block of generic function
#8354
Comments
I think the issue this runs into is:
To make it more problematic, substitute As you say, overloads will work for consumers, maybe for yourself try Edit: since, as below, that seems to make mypy quite unhappy, there's always manual dispatch... |
Ah, yeah I'd missed that subclasses might break that. |
Looks like
But type inference may be a little lacking:
|
It looks like there's nothing actionable here? |
I'm not sure if this is a bug or a known limitation. I think this is different to the other similar generics vs
isinstance
issues, but please do point me at another if this is a duplicate.Given some function:
I would hope that
mypy
can tell that, within theisinstance
block,T
can only bestr
(since that's whatval
is) and thus returning something which is explicitlystr
is fine.Unfortunately this case seems to error:
I realise this construct looks a bit odd in the cut-down above -- my actual use-case involves walking nested list and dictionary structures to modify values, but ends up with what I believe is the same error. Just to give a sense of that a slightly larger but still very cut-down version would be:
I realise I could use overloads to achieve a similar effect for consumers of the function, however I would also like to be able to type the implementation and get checking there.
In case it's useful, I'm using
mypy
version0.761
(latest at the time of writing) on Python 3.5.2.The text was updated successfully, but these errors were encountered: