Skip to content

fix: prevent false positive "untyped after decorator transformation" after deferral #19591

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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5433,7 +5433,7 @@ def visit_with_stmt(self, s: WithStmt) -> None:
self.accept(s.body)

def check_untyped_after_decorator(self, typ: Type, func: FuncDef) -> None:
if not self.options.disallow_any_decorated or self.is_stub:
if not self.options.disallow_any_decorated or self.is_stub or self.current_node_deferred:
return

if mypy.checkexpr.has_any_type(typ):
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,38 @@ def f(x: Any) -> Any: # E: Function is untyped after decorator transformation
def h(x): # E: Function is untyped after decorator transformation
pass
[builtins fixtures/list.pyi]

[case testDisallowAnyDecoratedUnannotatedDecoratorDeferred1]
# flags: --disallow-any-decorated

def d(f):
return f

def wrapper() -> None:
if c:
@d
def h(x): # E: Function is untyped after decorator transformation
pass

c = [1]
[builtins fixtures/list.pyi]
Copy link
Member

Choose a reason for hiding this comment

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

Although I think the fix looks right, I don't understand the logic of the tests: shouldn't we check that there is no false positive if the decorator is precisely typed (and there is a deferral)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Whoops, thanks! Fixed


[case testDisallowAnyDecoratedUnannotatedDecoratorDeferred2]
# flags: --disallow-any-decorated

def d(f):
return f

c = 1 # no deferral - check that the previous testcase is valid

def wrapper() -> None:
if c:
@d
def h(x): # E: Function is untyped after decorator transformation
pass

[builtins fixtures/list.pyi]

[case testDisallowAnyDecoratedErrorIsReportedOnlyOnce]
# flags: --disallow-any-decorated

Expand Down
Loading