-
-
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
mypy 0.990 crashes with segmentation fault on hydra_zen
due to recursive types
#14031
Comments
cc @ilevkivskyi |
Hm, I have a possible fix for this --- a/mypy/constraints.py
+++ b/mypy/constraints.py
@@ -177,7 +177,7 @@ def infer_constraints(template: Type, actual: Type, direction: int) -> list[Cons
for (t, a) in reversed(TypeState.inferring)
):
return []
- if has_recursive_types(template):
+ if has_recursive_types(template) or isinstance(template, Instance):
# This case requires special care because it may cause infinite recursion.
if not has_type_vars(template):
# Return early on an empty branch.
Now need to find a more compact repro for a regression test. Unfortunately, as with the previous crash I fixed, this will likely cause some performance hit. I will see if I can come up with something better, since this may affect performance of non-recursive types as well. I can probably give a bit more context on all the crashes. There is a classic algorithm for recursive types subtyping, it is however at least quadratic in number of type components of the recursive type, plus it slows down non-recursive types as well. So I tried to play smart, like "why would we need to push trivial pairs like (int, str) on an assumption stack, right?" It turns out there are various unexpected corner cases that are hard to predict where my optimization breaks (e.g. |
And here is a simple repro: from typing import Sequence, TypeVar
T = TypeVar("T")
def foo(x: T) -> str:
return "test"
Nested = str | Sequence[Nested]
x: Nested = foo(1) |
It turns out the above proposed fix doesn't cause any visible performance regression on self check #14038 |
Crash Report
Upgrading from mypy 0.982 to 0.990 leads to a segmentation fault when I run on a file that merely imports hydra_zen:
This appears to be related to hydra-zen's use of recursive types and mypy 0.990 enabling these by default. To confirm, I reverted to
0.982
and configured thenenable_recursive_aliases = true
and confirmed that this also produces a segmentation fault.This is the recursive type that is creating the issue. Oddly, when I specifically remove
Sequence["SupportedPrimitive"]
from the union (comment out line 223), the issue goes away. I spent time trying to further isolate the culprit and create a minimal example, but I was unable to decouple the issue from hydra_zen.Traceback
To Reproduce
Your Environment
0.990
mypy.ini
(and other config files): (None)python-3.9.13-haa1d7c7_2
The text was updated successfully, but these errors were encountered: