Skip to content

[ty] Allow classes to inherit from type[Any] or type[Unknown]#18060

Merged
AlexWaygood merged 1 commit intomainfrom
alex/type-any-inheritance
May 13, 2025
Merged

[ty] Allow classes to inherit from type[Any] or type[Unknown]#18060
AlexWaygood merged 1 commit intomainfrom
alex/type-any-inheritance

Conversation

@AlexWaygood
Copy link
Member

Summary

This continues our recent work to pay more respect to the gradual guarantee when inferring the MROs of classes. Classes are now permitted to inherit from type[Any] and type[Unknown]: since these are actually intersection types, the same principle applies to these types as was established in #18055.

Test Plan

mdtests added.

I wish I could tell you why the order of the diagnostics in the snapshot is changing as a result of these changes, but -- alas! -- I cannot. It doesn't seem like a massive problem, though.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label May 13, 2025
@AlexWaygood AlexWaygood force-pushed the alex/type-any-inheritance branch from 7935761 to cc252d1 Compare May 13, 2025 00:08
@github-actions
Copy link
Contributor

mypy_primer results

Changes were detected when running on open source projects
ignite (https://github.com/pytorch/ignite)
+ warning[unused-ignore-comment] ignite/distributed/auto.py:351:75: Unused blanket `type: ignore` directive
- Found 2276 diagnostics
+ Found 2277 diagnostics

operator (https://github.com/canonical/operator)
- error[invalid-base] ops/_private/harness.py:430:26: Invalid class base with type `type[Unknown]` (all bases must be a class, `Any`, `Unknown` or `Todo`)
- Found 203 diagnostics
+ Found 202 diagnostics

mypy (https://github.com/python/mypy)
+ warning[unused-ignore-comment] mypy/stubtest.py:435:35: Unused blanket `type: ignore` directive
- Found 3371 diagnostics
+ Found 3372 diagnostics

dd-trace-py (https://github.com/DataDog/dd-trace-py)
- error[invalid-super-argument] ddtrace/settings/_config.py:768:20: `type[Unknown]` is not a valid class
- error[invalid-super-argument] ddtrace/settings/_config.py:773:20: `type[Unknown]` is not a valid class
- Found 8604 diagnostics
+ Found 8602 diagnostics

scipy (https://github.com/scipy/scipy)
- error[invalid-super-argument] scipy/stats/_continuous_distns.py:68:20: `type[Unknown]` is not a valid class
- error[invalid-super-argument] scipy/stats/_continuous_distns.py:6765:20: `type[Unknown]` is not a valid class
- error[invalid-super-argument] scipy/stats/_continuous_distns.py:8568:20: `type[Unknown]` is not a valid class
- error[invalid-super-argument] scipy/stats/tests/test_distributions.py:295:21: `type[Unknown]` is not a valid class
- Found 7733 diagnostics
+ Found 7729 diagnostics

@AlexWaygood
Copy link
Member Author

That's quite interesting -- a lot of the primer reports are to do with super() rather than inheritance, as our BoundSuper type also users ClassBase as its inner data:

/// Represent a bound super object like `super(PivotClass, owner)`
#[salsa::interned(debug)]
pub struct BoundSuperType<'db> {
pub pivot_class: ClassBase<'db>,
#[return_ref]
pub owner: SuperOwnerKind<'db>,
}

I think the same arguments about the gradual guarantee apply to super() usage on type[Any] as they do to inheritance from type[Any], though. So this doesn't seem like a problem to me.

(The hits in operator and mypy are both inheritance-related; not all the primer hits are super()-related.)

@AlexWaygood AlexWaygood merged commit 7e9b0df into main May 13, 2025
35 checks passed
@AlexWaygood AlexWaygood deleted the alex/type-any-inheritance branch May 13, 2025 00:30
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
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.

2 participants