-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow assigning ellipsis literal as parameter default value #14982
Conversation
|
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
The pre-commit failure will go away if you merge in |
8157331
to
a7e7723
Compare
Wow there's a bug in the new github UI for PRs it was not showing the rebase button I thought it's already up to date. |
Oh, that only appears for a repository if the maintainers have checked a certain box in the repository settings on GitHub. We haven't enabled it for Astral repositories because the rate of development is quite rapid, and some folks found it annoying to be constantly told that all their PRs were out of date with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!!
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
Thanks for the review. |
4d835da
to
06e1551
Compare
Ah, I think there's a small misunderstanding here. There are two halves of typing.readthedocs.io:
The two pages you link to there are both in the "second half" of the website: those paragraphs describe best practices for people writing stub files, but these aren't necessarily rules that type checkers have to enforce regarding stub files. The typing spec part of typing.readthedocs.io is here: https://typing.readthedocs.io/en/latest/spec/distributing.html#value-expressions. It states that type checkers should allow
And later:
|
So I think we should allow |
Thanks for that clarification, Alex! I was surprised that I feel like the wording of the spec:
Suggests a totally different implementation approach here, which is that in a stub file we should simply infer literal |
That's very interesting, good observation! I don't actually know why that is. It may be in order to support ellipsis defaults in overloads and protocol methods. I'm a little surprised that this appears to be handled by allowing it based on the body being I'm not convinced we want to follow suit here; it seems incorrect to do this based on a body of So I would say let's implement this only for stubs for now, and consider the best approach when we add overloads/protocols support. |
That seems okay to me as long as we don't start inferring unions with |
Yes, that's right. |
Thanks let me do try to change this to infer the type of ellipsis literal as Any instead of adding this special case. |
Ok, after further discussion (thanks @AlexWaygood!) I think this is not a good idea, and might cause undesirable behavior in examples like this: class Foo: ...
X: int = ...
Y: Foo = X Where this should be flagged as an error, even in a type stub, but my suggestion (combined with our current handling of inferred dynamic type) would not consider it an error. (It might be worth including this as a test case.) So I think the better approach is in fact to special-case handling of literal In the first two cases, if we see a literal |
06e1551
to
9689544
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! A few comments.
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
annotation_ty, | ||
value_ty, | ||
); | ||
if self.file().is_stub(self.db().upcast()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we've forgotten the condition "and the value
expression is a literal ...
" here. If that doesn't fail any test, we should definitely add a test with e.g. x: int = 1
in a stub file showing we still infer Literal[1]
for x
, not Unknown
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it was not failing. Added a new test for it in mdtest/assignment/annotations.md
.
acf3072
to
d30649c
Compare
d499b43
to
2be530d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks close! A few comments below
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
Outdated
Show resolved
Hide resolved
…s.md Co-authored-by: Alex Waygood <[email protected]>
…s.md Co-authored-by: Alex Waygood <[email protected]>
…s.md Co-authored-by: Alex Waygood <[email protected]>
…s.md Co-authored-by: Alex Waygood <[email protected]>
…s.md Co-authored-by: Alex Waygood <[email protected]>
crates/red_knot_python_semantic/resources/mdtest/assignment/annotations.md
Outdated
Show resolved
Hide resolved
…notations.md Co-authored-by: Carl Meyer <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, modulo a couple small nits
Co-authored-by: Carl Meyer <[email protected]>
Co-Authored-by: Alex Waygood <[email protected]>
There was a problem hiding this 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!
Thanks for your time and review. |
* main: (60 commits) [`ruff`] Dataclass enums (`RUF049`) (#15299) Better error message when `--config` is given a table key and a non-inline-table value (#15266) Update pre-commit dependencies (#15289) Don't fix in ecosystem check (#15267) Update Rust crate itertools to 0.14.0 (#15287) Remove accidental empty block at the bottom of `split-static-string (SIM905)` doc (#15290) Update Rust crate clearscreen to v4 (#15288) Update Rust crate insta to v1.42.0 (#15286) Update NPM Development dependencies (#15285) Update dependency uuid to v11.0.4 (#15284) Update dependency ruff to v0.8.6 (#15283) Update Rust crate syn to v2.0.95 (#15282) Update Rust crate matchit to v0.8.6 (#15281) Update Rust crate bstr to v1.11.3 (#15280) [red-knot] Future-proof `Type::is_disjoint_from()` (#15262) [red-knot] Improve `Type::is_disjoint_from()` for `KnownInstanceType`s (#15261) [red-knot] Minor simplifications and improvements to constraint narrowing logic (#15270) Allow assigning ellipsis literal as parameter default value (#14982) [red-knot] fix control flow for assignment expressions in elif tests (#15274) [`refurb`] Mark fix as unsafe when the right-hand side is a string (`FURB171`) (#15273) ...
* main: (29 commits) [`ruff`] Dataclass enums (`RUF049`) (#15299) Better error message when `--config` is given a table key and a non-inline-table value (#15266) Update pre-commit dependencies (#15289) Don't fix in ecosystem check (#15267) Update Rust crate itertools to 0.14.0 (#15287) Remove accidental empty block at the bottom of `split-static-string (SIM905)` doc (#15290) Update Rust crate clearscreen to v4 (#15288) Update Rust crate insta to v1.42.0 (#15286) Update NPM Development dependencies (#15285) Update dependency uuid to v11.0.4 (#15284) Update dependency ruff to v0.8.6 (#15283) Update Rust crate syn to v2.0.95 (#15282) Update Rust crate matchit to v0.8.6 (#15281) Update Rust crate bstr to v1.11.3 (#15280) [red-knot] Future-proof `Type::is_disjoint_from()` (#15262) [red-knot] Improve `Type::is_disjoint_from()` for `KnownInstanceType`s (#15261) [red-knot] Minor simplifications and improvements to constraint narrowing logic (#15270) Allow assigning ellipsis literal as parameter default value (#14982) [red-knot] fix control flow for assignment expressions in elif tests (#15274) [`refurb`] Mark fix as unsafe when the right-hand side is a string (`FURB171`) (#15273) ...
Resolves #14840
Summary
Usage of ellipsis literal as default argument is allowed in stub files.
Test Plan
Added mdtest for both python files and stub files.