Skip to content

[ty] Fix normalization of unions containing instances parameterized with unions #18112

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 1 commit into from
May 15, 2025

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented May 15, 2025

Summary

  • Recurse into the specialisations of specialised generics when normalizing them, in order to ensure that Foo[int | str] and Foo[str | int] have the same Salsa ID when normalized
  • Normalize Unknown, Todo and Any all to Any. I'll be honest here: I still don't totally understand why this is necessary, but I can say that it makes the nondeterministic behaviour on hydra-zen go away. And I think it may be a good thing to do anyway:
    • It makes these types easier to reason about when they appear in unions/intersections
    • It reflects the fact that these types are in fact gradually equivalent.

This is stacked on top of #18111, because otherwise it causes some small regressions in our redundant-cast mdtests because of the new normalisations of dynamic types.

Fixes astral-sh/ty#369

Test Plan

  • Added mdtests for the issue with instances of specialised generics
  • Repeatedly ran this branch on hydra-zen using the setup described in Non-deterministic output on hydra-zen ty#369 (comment) and observed that there was no longer any non-deterministic behaviour when checking hydra-zen (and I can reproduce the non-deterministic behaviour on main using that setup)

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label May 15, 2025
@AlexWaygood AlexWaygood added the bug Something isn't working label May 15, 2025
Copy link
Contributor

github-actions bot commented May 15, 2025

mypy_primer results

Changes were detected when running on open source projects
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- error[invalid-return-type] src/hydra_zen/wrapper/_implementations.py:945:16: Return type does not match returned value: expected `DataClass_`, found `@Todo(unsupported type[X] special form) | (((...) -> Any) & dict[Unknown, Unknown]) | (DataClass_ & dict[Unknown, Unknown]) | (list[Any] & dict[Unknown, Unknown]) | dict[Any, Any] | (((...) -> Any) & list[Unknown]) | (DataClass_ & list[Unknown]) | list[Any] | (dict[Any, Any] & list[Unknown])`
+ error[invalid-return-type] src/hydra_zen/wrapper/_implementations.py:945:16: Return type does not match returned value: expected `DataClass_`, found `@Todo(unsupported type[X] special form) | (((...) -> Any) & dict[Unknown, Unknown]) | (DataClass_ & dict[Unknown, Unknown]) | (list[Any] & dict[Unknown, Unknown]) | dict[Any, Any] | (((...) -> Any) & list[Unknown]) | (DataClass_ & list[Unknown]) | list[Any]`
- error[type-assertion-failure] tests/annotations/declarations.py:969:5: Argument does not have asserted type `FullBuilds[@Todo(Support for `typing.TypeAlias`)] | PBuilds[@Todo(Support for `typing.TypeAlias`)] | StdBuilds[@Todo(Support for `typing.TypeAlias`)]`
- error[type-assertion-failure] tests/annotations/declarations.py:980:5: Argument does not have asserted type `FullBuilds[@Todo(Support for `typing.TypeAlias`)] | PBuilds[@Todo(Support for `typing.TypeAlias`)] | StdBuilds[@Todo(Support for `typing.TypeAlias`)]`
- Found 645 diagnostics
+ Found 643 diagnostics

@AlexWaygood AlexWaygood force-pushed the alex/gradual-normalize branch 2 times, most recently from 7321a11 to 6916b39 Compare May 15, 2025 01:55
Base automatically changed from alex/redundant-cast-unknown to main May 15, 2025 02:38
@AlexWaygood AlexWaygood force-pushed the alex/gradual-normalize branch from 6916b39 to 19fe882 Compare May 15, 2025 02:39
@AlexWaygood AlexWaygood force-pushed the alex/gradual-normalize branch from 19fe882 to fb190e8 Compare May 15, 2025 02:43
@AlexWaygood AlexWaygood reopened this May 15, 2025
@AlexWaygood AlexWaygood changed the title [ty] Fix normalization of unions containing instances generic over unions [ty] Fix normalization of unions containing instances parameterized with unions May 15, 2025
@AlexWaygood AlexWaygood merged commit c3a4992 into main May 15, 2025
34 checks passed
@AlexWaygood AlexWaygood deleted the alex/gradual-normalize branch May 15, 2025 02:48
@sharkdp
Copy link
Contributor

sharkdp commented May 15, 2025

Thank you @AlexWaygood.

With this PR, you fixed one particular problem in normalization. I'm not trying to imply that my attempt in #18091 is the right solution, but I think I'm not happy with the current state. There is nothing that prevents us from introducing similar problems in the future. And there is no guarantee that other problems already exist. In fact, it's easy to find other cases that are still problematic by embedding unions in types that we don't have any special handling for, yet. A lot of Type-variants contain Types further down in their structure, not just unions, intersections and tuples. Especially with the introduction of generics.

We have seen in astral-sh/ty#369 that this can lead to extremely subtle bugs (maybe we'll be more aware next time, but it took me several hours to find the root cause). So I with there would be some way we could get help from the compiler to get this right.

@AlexWaygood
Copy link
Member Author

@sharkdp yes, I agree! I think there are probably still other latent bugs with normalisation lurking in our codebase already. This PR fixes some specific issues with normalisation, but it doesn't fix the general problem that it's hard to get this approach right.

I'd also love to see if we can explore ways to make our design more robust. I liked the idea you mentioned on Discord the other day of having a generalised visitor pattern for recursing into types and applying transformations to all the sub-nodes — it's possible that might make this easier to get right?

Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Non-deterministic output on hydra-zen
3 participants