Skip to content
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

[red-knot] Disallow more invalid type expressions #16427

Merged
merged 1 commit into from
Feb 28, 2025

Conversation

AlexWaygood
Copy link
Member

Summary

As part of working on #16302, I'm auditing all uses of Type::to_instance(). I noticed that one use in Type::in_type_expression has a pretty clear bug, in that it treats Type::SubclassOf() the same as Type::ClassLiteral. That's incorrect: an object can only have a type[] type if it's a dynamic variable of some kind, and we should reject such variables in type expressions. The practical effect of this is that on main, we have this incorrect behaviour, where we should be reject y's type annotation altogether, but instead we accept it:

def _(x: type[int]):
    def f(y: x):
        reveal_type(y)  # revealed: int

This PR fixes the bug by making Type::in_type_expression() exhaustive over all Type variants and rejecting most of them when they appear in type expressions. (The method is not yet exhaustive over all KnownInstanceType variants; some of these would require special error messages, and it seemed out of the scope of this bugfix PR.)

Test Plan

Added mdtests.

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.

Nice!

@AlexWaygood AlexWaygood force-pushed the alex/reject-bad-type-exprs branch 2 times, most recently from 1f5f98b to 2809aa4 Compare February 28, 2025 09:59
@AlexWaygood AlexWaygood force-pushed the alex/reject-bad-type-exprs branch from 2809aa4 to e1bb7b1 Compare February 28, 2025 10:00
@AlexWaygood AlexWaygood enabled auto-merge (squash) February 28, 2025 10:01
@AlexWaygood AlexWaygood merged commit 09d0b22 into main Feb 28, 2025
20 checks passed
@AlexWaygood AlexWaygood deleted the alex/reject-bad-type-exprs branch February 28, 2025 10:04
dcreager added a commit that referenced this pull request Feb 28, 2025
* main:
  [red-knot] Switch to a handwritten parser for mdtest error assertions (#16422)
  [red-knot] Disallow more invalid type expressions (#16427)
  Bump version to Ruff 0.9.9 (#16434)
  Check `LinterSettings::preview` for version-related syntax errors (#16429)
  Avoid caching files with unsupported syntax errors (#16425)
  Prioritize "bug" label for changelog sections (#16433)
  [`flake8-copyright`] Add links to applicable options (`CPY001`) (#16421)
  Fix string-length limit in documentation for PYI054 (#16432)
  Show version-related syntax errors in the playground (#16419)
  Allow passing `ParseOptions` to inline tests (#16357)
  Bump version to 0.9.8 (#16414)
  [red-knot] Ignore surrounding whitespace when looking for `<!-- snapshot-diagnostics -->` directives in mdtests (#16380)
  Notify users for invalid client settings (#16361)
  Avoid indexing the project if `configurationPreference` is `editorOnly` (#16381)
dcreager added a commit that referenced this pull request Feb 28, 2025
* dcreager/alist-type:
  Remove unneeded shear override
  Update property test CI
  Move alist into red_knot_python_semantic
  [red-knot] Switch to a handwritten parser for mdtest error assertions (#16422)
  [red-knot] Disallow more invalid type expressions (#16427)
  Bump version to Ruff 0.9.9 (#16434)
  Check `LinterSettings::preview` for version-related syntax errors (#16429)
  Avoid caching files with unsupported syntax errors (#16425)
  Prioritize "bug" label for changelog sections (#16433)
  [`flake8-copyright`] Add links to applicable options (`CPY001`) (#16421)
  Fix string-length limit in documentation for PYI054 (#16432)
  Show version-related syntax errors in the playground (#16419)
  Allow passing `ParseOptions` to inline tests (#16357)
  Bump version to 0.9.8 (#16414)
  [red-knot] Ignore surrounding whitespace when looking for `<!-- snapshot-diagnostics -->` directives in mdtests (#16380)
  Notify users for invalid client settings (#16361)
  Avoid indexing the project if `configurationPreference` is `editorOnly` (#16381)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants