-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[red-knot] Trust module-level undeclared symbols in stubs #17577
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
Conversation
|
234bc3a
to
b68e294
Compare
b68e294
to
bf60832
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.
Yeah, this makes sense to me.
I do still think a lot of these assignments should probably be annotated with Final
in typeshed anyway! This would benefit both us and other type checkers.
But your ecosystem analysis indicates that this is also a broader problem than just typeshed: attrs
also appears to use bare assignments without declarations in its stub files, etc.
@@ -593,8 +593,18 @@ fn symbol_by_id<'db>( | |||
"__slots__" | "TYPE_CHECKING" | |||
); | |||
|
|||
widen_type_for_undeclared_public_symbol(db, inferred, is_considered_non_modifiable) | |||
.into() | |||
if scope.is_module_scope(db) && scope.file(db).is_stub(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.
why only at the module level? Typeshed does things like this as well, and I think there's an argument to be made that the public type for DeprecatedNameForThing
should also not be unioned with Unknown
here:
class Foo:
THING: Final = 42
DeprecatedNameForThing = THING
it feels consistent with the idea behind this PR.
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.
I didn't have this restriction initially, but then added it because Carl suggested this as a rule here: #17032 (comment). What was your reasoning @carljm?
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.
I'm going to do the following: I'm going to merge this as-is, and then re-open another PR with that restriction lifted. This should allow us to see the impact in a better way.
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.
Sounds good!
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.
I do think the same rationale applies; I don't think I carefully thought through the class-scope case as even a thing that would happen in stub files.
bf60832
to
863a04a
Compare
…tructor * origin/main: [red-knot] Trust module-level undeclared symbols in stubs (#17577) [`airflow`] Apply auto fixes to cases where the names have changed in Airflow 3 (`AIR301`) (#17355) [`pycodestyle`] Auto-fix redundant boolean comparison (`E712`) (#17090) [red-knot] Detect semantic syntax errors (#17463) Fix stale diagnostics in Ruff playground (#17583) [red-knot] Early return from `project.is_file_open` for vendored files (#17580)
## Summary *Generally* trust undeclared symbols in stubs, not just at the module level. Follow-up on the discussion [here](#17577 (comment)). ## Test Plan New Markdown test.
…var-instance * dcreager/generic-constructor: Revert FunctionLiteral type [red-knot] Trust module-level undeclared symbols in stubs (#17577) [`airflow`] Apply auto fixes to cases where the names have changed in Airflow 3 (`AIR301`) (#17355) [`pycodestyle`] Auto-fix redundant boolean comparison (`E712`) (#17090) Clean this up a bit clippy [red-knot] Detect semantic syntax errors (#17463) Fix stale diagnostics in Ruff playground (#17583) [red-knot] Early return from `project.is_file_open` for vendored files (#17580)
Summary
Many symbols in typeshed are defined without being declared. For example:
Here, we introduce a change that skips widening the public type of these symbols (by unioning with
Unknown
).fixes #17032
Ecosystem analysis
This is difficult to analyze in detail, but I went over most changes and it looks very favorable to me overall. The diff on the overall numbers is:
Removed false positives
invalid-base
examples:invalid-exception-caught
examples:unresolved-reference
exampleshttps://github.com/canonical/cloud-init/blob/7a0265d36e01e649f72005548f17dca9ac0150ad/cloudinit/handlers/jinja_template.py#L120-L123 (we now understand the
isinstance
narrowing)- error[lint:unresolved-attribute] /tmp/mypy_primer/projects/cloud-init/cloudinit/handlers/jinja_template.py:123:16: Type `Exception` has no attribute `errno`
unknown-argument
exampleshttps://github.com/hauntsaninja/boostedblob/blob/master/boostedblob/request.py#L53
- error[lint:unknown-argument] /tmp/mypy_primer/projects/boostedblob/boostedblob/request.py:53:17: Argument `connect` does not match any known parameter of bound method `__init__`
unknown-argument
There are a lot of
__init__
-related changes because we now understand@attr.s
as a@dataclass_transform
annotated symbol. For example:- error[lint:unknown-argument] /tmp/mypy_primer/projects/attrs/tests/test_hooks.py:72:18: Argument `x` does not match any known parameter of bound method `__init__`
New false positives
This can happen if a symbol that previously was inferred as
X | Unknown
was assigned-to, but we don't yet understand the assignability toX
:https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/exceptions/handler.py#L90
+ error[lint:invalid-assignment] /tmp/mypy_primer/projects/strawberry/strawberry/exceptions/handler.py:90:9: Object of type `def strawberry_threading_exception_handler(args: tuple[type[BaseException], BaseException | None, TracebackType | None, Thread | None]) -> None` is not assignable to attribute `excepthook` of type `(_ExceptHookArgs, /) -> Any`
New true positives
https://github.com/DataDog/dd-trace-py/blob/6bbb5519fe4b3964f9ca73b21cf35df8387618b2/tests/tracer/test_span.py#L714
+ error[lint:invalid-argument-type] /tmp/mypy_primer/projects/dd-trace-py/tests/tracer/test_span.py:714:33: Argument to this function is incorrect: Expected `str`, found `Literal[b"\xf0\x9f\xa4\x94"]`
Changed diagnostics
A lot of changed diagnostics because we now show
@Todo(Support for
typing.TypeVarinstances in type expressions)
instead ofUnknown
for all kinds of symbols that used a_T = TypeVar("_T")
as a type. One prominent example is thelist.__getitem__
method:builtins.pyi
:which causes this change in diagnostics:
Test Plan
Updated Markdown tests