ruff icon indicating copy to clipboard operation
ruff copied to clipboard

[ty] Improve typevar solving when encountering unions of generic types

Open suleymanozkeskin opened this issue 1 month ago • 3 comments

Improves type inference for union types with multiple generic parameters like Ok[T] | Err[E] by adding union-pairing logic to the constraint solver.

Changes:

  • Added union element pairing with typevar confinement checks in generics.rs
  • Added 3 tests to type_guards.md

Summary

Following the explanatory comment, made by @carljm, I made some refactoring which I hope addresses the issue on the correct place.

I am not sure if this is still a right or suitable fix that matches the further plans of the maintainers on this topic.

Fixes https://github.com/astral-sh/ty/issues/1703

Test Plan

I used the existing markdown test suite with additional test cases to cover the issue I opened + the case mentioned here

suleymanozkeskin avatar Dec 05 '25 20:12 suleymanozkeskin

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

astral-sh-bot[bot] avatar Dec 05 '25 20:12 astral-sh-bot[bot]

mypy_primer results

Changes were detected when running on open source projects
beartype (https://github.com/beartype/beartype)
- beartype/claw/_package/clawpkgtrie.py:66:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieBlacklist]'> | <class 'dict[str, Divergent]'>`
- beartype/claw/_package/clawpkgtrie.py:247:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieWhitelist]'> | <class 'dict[str, Divergent]'>`
- Found 494 diagnostics
+ Found 492 diagnostics

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
+ src/scikit_build_core/build/_pathutil.py:25:38: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `str | PathLike[str]`, found `DirEntry[Path]`
+ src/scikit_build_core/build/_pathutil.py:27:24: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `str | PathLike[str]`, found `DirEntry[Path]`
+ src/scikit_build_core/build/wheel.py:98:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 39 diagnostics
+ Found 42 diagnostics

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- pandas-stubs/_typing.pyi:1217:16: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 5519 diagnostics
+ Found 5518 diagnostics


No memory usage changes detected ✅

astral-sh-bot[bot] avatar Dec 05 '25 20:12 astral-sh-bot[bot]

Thank you for your contribution. I'm planning to review this soon.

sharkdp avatar Dec 10 '25 19:12 sharkdp