Skip to content

Handle interactions between recursive aliases and recursive instances #13328

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

Merged
merged 7 commits into from
Aug 5, 2022

Conversation

ilevkivskyi
Copy link
Member

This is a follow-up for #13297

The fix for infinite recursion is kind of simple, but it is hard to make inference infer something useful. Currently we handle all most common cases, but it is quite fragile (I however have few tricks left if people will complain about inference).

Btw a curious thing: it looks like "hm..." is a valid value for Nested = Sequence[Union[int, Nested]].

cc @JukkaL

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Yeah, that is curious, but it makes sense (pyright also does the same thing)

@ilevkivskyi
Copy link
Member Author

ilevkivskyi commented Aug 5, 2022

I will add couple more cases with a bit more principled inference recursion detection. Btw there is a thing that complicates things that is not specific to recursive types (see e.g. #11149), when mypy sees things like Union[T, List[T]] :> List[int], it can't decide which one to choose T :> int or T :> List[int]. This can be solved by adding an upper bound for T which forces mypy to choose one. I expect this to appear more often for recursive types (as they are in 95% of cases union-heavy), so we may add workaround with upper bound to the "Common issues" section in the docs.

EDIT: fixed the direction when this causes problems

@ilevkivskyi
Copy link
Member Author

I actually added a bit of special-casing to handle the above problem specifically for recursive types. It should handle most of simple cases. I don't want to add this in general, as constraints inference is a very sensitive place.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@ilevkivskyi ilevkivskyi merged commit 608de81 into python:master Aug 5, 2022
@ilevkivskyi ilevkivskyi deleted the support-rec-instances branch August 5, 2022 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants