Skip to content

Conversation

@AlexWaygood
Copy link
Member

Summary

A small refactor followup to #21886. Now that we have a KnownUnion enum, we can use it in more places to make our code a bit more DRY

Test Plan

pure refactor; no added tests

@AlexWaygood AlexWaygood requested a review from carljm as a code owner December 12, 2025 14:01
@AlexWaygood AlexWaygood added the internal An internal refactor or improvement label Dec 12, 2025
@AlexWaygood AlexWaygood requested a review from sharkdp as a code owner December 12, 2025 14:01
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Dec 12, 2025
Comment on lines -7299 to 7290
let ty = match class.known(db) {
Some(KnownClass::Complex) => UnionType::from_elements(
db,
[
KnownClass::Int.to_instance(db),
KnownClass::Float.to_instance(db),
KnownClass::Complex.to_instance(db),
],
),
Some(KnownClass::Float) => UnionType::from_elements(
db,
[
KnownClass::Int.to_instance(db),
KnownClass::Float.to_instance(db),
],
),
_ if class.is_typed_dict(db) => {
Type::typed_dict(class.default_specialization(db))
}
Some(KnownClass::Complex) => KnownUnion::Complex.to_type(db),
Some(KnownClass::Float) => KnownUnion::Float.to_type(db),
_ => Type::instance(db, class.default_specialization(db)),
};
Ok(ty)
}
Type::GenericAlias(alias) if alias.is_typed_dict(db) => Ok(Type::typed_dict(*alias)),
Type::GenericAlias(alias) => Ok(Type::instance(db, ClassType::from(*alias))),
Copy link
Member Author

Choose a reason for hiding this comment

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

whether or not the class is a TypedDict is also checked in the Type::instance() call. Checking whether the class-literal/generic-alias is a TypedDict before calling Type::instance() is redundant.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 12, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 12, 2025

mypy_primer results

Changes were detected when running on open source projects
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/build/wheel.py:98:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 42 diagnostics
+ Found 41 diagnostics

No memory usage changes detected ✅

@AlexWaygood AlexWaygood enabled auto-merge (squash) December 12, 2025 14:06
@AlexWaygood AlexWaygood merged commit ff0ed4e into main Dec 12, 2025
43 checks passed
@AlexWaygood AlexWaygood deleted the alex/knownunion branch December 12, 2025 14:06
dcreager added a commit that referenced this pull request Dec 13, 2025
* origin/main: (22 commits)
  [ty] Allow gradual lower/upper bounds in a constraint set (#21957)
  [ty] disallow explicit specialization of type variables themselves (#21938)
  [ty] Improve diagnostics for unsupported binary operations and unsupported augmented assignments (#21947)
  [ty] update implicit root docs (#21955)
  [ty] Enable even more goto-definition on inlay hints (#21950)
  Document known lambda formatting deviations from Black (#21954)
  [ty] fix hover type on named expression target (#21952)
  Bump benchmark dependencies (#21951)
  Keep lambda parameters on one line and parenthesize the body if it expands (#21385)
  [ty] Improve resolution of absolute imports in tests (#21817)
  [ty] Support `__all__ += submodule.__all__`
  [ty] Change frequency of invalid `__all__` debug message
  [ty] Add `KnownUnion::to_type()` (#21948)
  [ty] Classify `cls` as class parameter (#21944)
  [ty] Stabilize rename (#21940)
  [ty] Ignore `__all__` for document and workspace symbol requests
  [ty] Attach db to background request handler task (#21941)
  [ty] Fix outdated version in publish diagnostics after `didChange` (#21943)
  [ty] avoid fixpoint unioning of types containing current-cycle Divergent (#21910)
  [ty] improve bad specialization results & error messages (#21840)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants