Skip to content

[red-knot] Emit error if int/float/complex/bytes/boolean literals appear in type expressions outside typing.Literal[] #16765

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

Merged

Conversation

MatthewMckee4
Copy link
Contributor

Expand InvalidTypeExpression enum to include number, bytes and boolean in type expression

Summary

Fixes #16532

Test Plan

Still need to look into where tests should be put for this improvement

Expand `InvalidTypeExpression` enum to include number, bytes and boolean in type expression
@MatthewMckee4
Copy link
Contributor Author

I may be wrong, but it seems that byte literals are already handled here:

ast::Expr::BytesLiteral(bytes) => {
self.context.report_lint(
&BYTE_STRING_TYPE_ANNOTATION,
bytes,
format_args!("Type expressions cannot use bytes literal"),
);
TypeAndQualifiers::unknown()
}
.

And tested here:

# error: [byte-string-type-annotation] "Type expressions cannot use bytes literal"
i: "b'int'",

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Mar 15, 2025
@MatthewMckee4
Copy link
Contributor Author

There's now a test failing at

I think this is expected though, as the generics are not implemented for SpecialForm i think?

@MatthewMckee4 MatthewMckee4 marked this pull request as ready for review March 16, 2025 12:41
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks! I think you're mostly on the right track, but there's a few issues here to sort out

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Looks close!

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

looks great, thank you!

@AlexWaygood
Copy link
Member

I'll wait until our CI's back up and running before merging this (everything's disabled at the moment due to #16768, we should get it up again tomorrow)

@MatthewMckee4
Copy link
Contributor Author

Sounds good, thank you for the feedback!

@MichaReiser MichaReiser reopened this Mar 17, 2025
Copy link
Contributor

github-actions bot commented Mar 17, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

github-actions bot commented Mar 17, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@AlexWaygood AlexWaygood changed the title Add invalid type expressions. [red-knot] Emit error if int/float/complex/bytes/boolean literals appear in type expressions outside typing.Literal[] Mar 17, 2025
@AlexWaygood AlexWaygood enabled auto-merge (squash) March 17, 2025 11:23
@AlexWaygood AlexWaygood force-pushed the literal-types-invalid-type-expression branch from ecfe1ac to 74d6751 Compare March 17, 2025 11:24
@AlexWaygood AlexWaygood force-pushed the literal-types-invalid-type-expression branch from 74d6751 to 9c36f0f Compare March 17, 2025 11:27
@AlexWaygood AlexWaygood disabled auto-merge March 17, 2025 11:31
@AlexWaygood AlexWaygood enabled auto-merge (squash) March 17, 2025 11:52
@AlexWaygood AlexWaygood merged commit 2470777 into astral-sh:main Mar 17, 2025
21 checks passed
dcreager added a commit that referenced this pull request Mar 17, 2025
* main: (25 commits)
  [syntax-errors] Parenthesized context managers before Python 3.9 (#16523)
  [ci]: Disable wheel testing on `ppc64le` (#16793)
  [red-knot] Stabilize `negation_reverses_subtype_order` property test (#16801)
  [red-knot] Emit error if int/float/complex/bytes/boolean literals appear in type expressions outside `typing.Literal[]` (#16765)
  [ci] Use `git diff` instead of `changed-files` GH action (#16796)
  [syntax-errors] Improve error message and range for pre-PEP-614 decorator syntax errors (#16581)
  [`flake8-bandit`] Allow raw strings in `suspicious-mark-safe-usage` (`S308`) #16702 (#16770)
  [`refurb`] Avoid panicking `unwrap` in `verbose-decimal-constructor` (`FURB157`) (#16777)
  [red-knot] Add `--color` CLI option (#16758)
  [internal]: Upgrade salsa (#16794)
  Pin dependencies (#16791)
  [internal]: Update indirect dependencies (#16792)
  [ci]: Fixup codspeed upgrade (#16790)
  Update Rust crate compact_str to 0.9.0 (#16785)
  Update Rust crate clap to v4.5.32 (#16778)
  Update Rust crate codspeed-criterion-compat to v2.9.1 (#16784)
  Update Rust crate quote to v1.0.40 (#16782)
  Update Rust crate ordermap to v0.5.6 (#16781)
  Update cloudflare/wrangler-action action to v3.14.1 (#16783)
  Update Rust crate env_logger to v0.11.7 (#16779)
  ...
@MatthewMckee4 MatthewMckee4 deleted the literal-types-invalid-type-expression branch March 17, 2025 20:55
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.

[red-knot] Raise "invalid type expression" error for literal types
4 participants