Skip to content

Conversation

@AlexWaygood
Copy link
Member

Summary

Stacked on top of #18053. This fixes a new false positive I can see in the primer diff on that PR, and I think it's a good change to make anyway!

Test Plan

mdtests added

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label May 12, 2025
@github-actions
Copy link
Contributor

mypy_primer results

Changes were detected when running on open source projects
sympy (https://github.com/sympy/sympy)
+ warning[unused-ignore-comment] sympy/parsing/autolev/_listener_autolev_antlr.py:402:41: Unused blanket `type: ignore` directive
- error[invalid-base] sympy/utilities/decorator.py:319:27: Invalid class base with type `Unknown & <Protocol with members '__mro__'>` (all bases must be a class, `Any`, `Unknown` or `Todo`)

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Thank you!

Self::try_from_type(db, todo_type!("GenericAlias instance"))
}
Type::Intersection(inter) => {
let dynamic_element = inter
Copy link
Contributor

Choose a reason for hiding this comment

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

This change looks good, but is there any reason it should be specific to a dynamic element? It seems like as long as the intersection is not disjoint from type, and we can find at least one element in it that is a valid base, we should be able to safely consider it that type and construct the base accordingly.

(Not saying that change needs to happen in this PR -- but the thought could maybe go into a comment, to help us out if it comes up in future.)

Copy link
Member Author

Choose a reason for hiding this comment

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

That's... an interesting thought. I'm not sure it has any practical consequences right now, because the only other "atomic" types we support as class bases currently are class-literal types and dynamic types. <dynamic type> & whatever is handled here. <class literal type> & whatever should always either simplify to <class literal type> or Never, since class-literal types only have a single inhabitant.

(We probably should allow users to inherit from objects of type[Any], though, due to it actually being an intersection. And if we did that, then yes, this would have more practical consequences. I can do that in a followup. And I'll tackle this at the same time!)

Base automatically changed from alex/hasattr-narrow to main May 12, 2025 22:58
…n contains a dynamic type and the intersection is not disjoint from `type`
@AlexWaygood AlexWaygood force-pushed the alex/invalid-base-intersections branch from 05061ac to 067273b Compare May 12, 2025 23:03
@AlexWaygood AlexWaygood enabled auto-merge (squash) May 12, 2025 23:04
@AlexWaygood AlexWaygood merged commit 41fa082 into main May 12, 2025
31 checks passed
@AlexWaygood AlexWaygood deleted the alex/invalid-base-intersections branch May 12, 2025 23:07
dcreager added a commit that referenced this pull request May 13, 2025
…eepish

* origin/main:
  [ty] Induct into instances and subclasses when finding and applying generics (#18052)
  [ty] Allow classes to inherit from `type[Any]` or `type[Unknown]` (#18060)
  [ty] Allow a class to inherit from an intersection if the intersection contains a dynamic type and the intersection is not disjoint from `type` (#18055)
  [ty] Narrowing for `hasattr()` (#18053)
  Update reference documentation for `--python-version` (#18056)
  [`flake8-bugbear`] Ignore `B028` if `skip_file_prefixes` is present (#18047)
  [`airflow`] Apply try-catch guard to all AIR3 rules (`AIR3`) (#17887)
  [`pylint`] add fix safety section (`PLW3301`) (#17878)
  Update `--python` to accept paths to executables in virtual environments (#17954)
  [`pylint`] add fix safety section (`PLE4703`) (#17824)
  [`ruff`] Implement a recursive check for `RUF060` (#17976)
  [`flake8-use-pathlib`] `PTH*` suppress diagnostic for all `os.*` functions that have the `dir_fd` parameter (#17968)
  [`refurb`] Mark autofix as safe only for number literals in `FURB116` (#17692)
  [`flake8-simplify`] Fix `SIM905` autofix for `rsplit` creating a reversed list literal (#18045)
  Avoid initializing progress bars early (#18049)
dcreager added a commit that referenced this pull request May 13, 2025
…eep-dish

* origin/main:
  [ty] Infer parameter specializations of generic aliases (#18021)
  [ty] Understand homogeneous tuple annotations (#17998)
  [ty] Induct into instances and subclasses when finding and applying generics (#18052)
  [ty] Allow classes to inherit from `type[Any]` or `type[Unknown]` (#18060)
  [ty] Allow a class to inherit from an intersection if the intersection contains a dynamic type and the intersection is not disjoint from `type` (#18055)
  [ty] Narrowing for `hasattr()` (#18053)
  Update reference documentation for `--python-version` (#18056)
  [`flake8-bugbear`] Ignore `B028` if `skip_file_prefixes` is present (#18047)
  [`airflow`] Apply try-catch guard to all AIR3 rules (`AIR3`) (#17887)
  [`pylint`] add fix safety section (`PLW3301`) (#17878)
  Update `--python` to accept paths to executables in virtual environments (#17954)
  [`pylint`] add fix safety section (`PLE4703`) (#17824)
  [`ruff`] Implement a recursive check for `RUF060` (#17976)
  [`flake8-use-pathlib`] `PTH*` suppress diagnostic for all `os.*` functions that have the `dir_fd` parameter (#17968)
  [`refurb`] Mark autofix as safe only for number literals in `FURB116` (#17692)
  [`flake8-simplify`] Fix `SIM905` autofix for `rsplit` creating a reversed list literal (#18045)
  Avoid initializing progress bars early (#18049)
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 21, 2025
…n contains a dynamic type and the intersection is not disjoint from `type` (astral-sh#18055)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants