pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Fix self type specialization in overloaded __init__ methods (#384)

Open officiallyutso opened this issue 6 months ago • 1 comments

Issue

Fixes #384

This PR addresses an issue with self type specialization in overloaded __init__ methods. The type checker was not properly handling specialized self types during constructor calls with generic classes.

Changes

  • Modified callable_infer in callable.rs to properly handle self type substitution for __init__ method calls
  • Added detection for __init__ method calls with self arguments
  • Implemented proper self type substitution in return types using subst_self_type_mut
  • Updated call handling to ensure correct type inference for constructor calls with specialized self types
  • Integrated with typeshed stubs to ensure compatibility with standard library patterns

Testing

Verified with test cases in constructors_call_init.py, particularly those involving classes with overloaded __init__ methods that specify specialized self types (e.g., Class5[list[int]], Class5[set[str]]).

Impact

This fix ensures that when a class defines overloaded __init__ methods with specialized self types, the type checker correctly resolves constructor calls to the expected specialized type, improving type inference accuracy for generic class instantiation.

officiallyutso avatar May 28 '25 09:05 officiallyutso

I would expect to see some conformance changes from fixing this issue, but I don't see conformance.exp changes - did you run test.py and commit the generated files?

Also, since this feature is tricky, please add integration tests here: https://github.com/facebook/pyrefly/blob/main/pyrefly/lib/test/overload.rs

yangdanny97 avatar May 29 '25 17:05 yangdanny97

Closing this PR due to inactivity. Feel free to reopen once you address the comments. Thanks!

yangdanny97 avatar Jun 16 '25 19:06 yangdanny97