Skip to content

Commit 1c92096

Browse files
committed
Do not report errors in untyped defs
1 parent 45ba182 commit 1c92096

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

mypy/checker.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,15 @@ def require_correct_self_argument(self, func: Type, defn: FuncDef) -> bool:
15411541
if not isinstance(func, CallableType):
15421542
return False
15431543

1544+
# Do not report errors for untyped methods in classes nested in untyped funcs.
1545+
if not (
1546+
self.options.check_untyped_defs
1547+
or len(self.dynamic_funcs) < 2
1548+
or not self.dynamic_funcs[-2]
1549+
or not defn.is_dynamic()
1550+
):
1551+
return bool(func.arg_types)
1552+
15441553
with self.scope.push_function(defn):
15451554
# We temporary push the definition to get the self type as
15461555
# visible from *inside* of this function/method.

test-data/unit/check-classes.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7911,6 +7911,30 @@ reveal_type(D().fn3) # E: Invalid self argument "D" to attribute function "fn3"
79117911
# N: Revealed type is "def (self: __main__.D, _x: builtins.int) -> builtins.int"
79127912
[builtins fixtures/tuple.pyi]
79137913

7914+
[case testMethodSelfArgumentChecksInUntyped]
7915+
def unchecked():
7916+
class Bad:
7917+
def fn() -> None: ... # E: Method must have at least one argument. Did you forget the "self" argument?
7918+
def fn2(x: int) -> None: ... # E: Self argument missing for a non-static method (or an invalid type for self)
7919+
7920+
class Ok:
7921+
def fn(): ...
7922+
def fn2(x): ...
7923+
7924+
def checked() -> None:
7925+
class Bad:
7926+
def fn() -> None: ... # E: Method must have at least one argument. Did you forget the "self" argument?
7927+
def fn2(x: int) -> None: ... # E: Self argument missing for a non-static method (or an invalid type for self)
7928+
7929+
class AlsoBad:
7930+
def fn(): ... # E: Method must have at least one argument. Did you forget the "self" argument?
7931+
def fn2(x): ...
7932+
7933+
class Ok:
7934+
def fn(): ... # E: Method must have at least one argument. Did you forget the "self" argument?
7935+
def fn2(x): ...
7936+
[builtins fixtures/tuple.pyi]
7937+
79147938
[case testTypeAfterAttributeAccessWithDisallowAnyExpr]
79157939
# flags: --disallow-any-expr
79167940

0 commit comments

Comments
 (0)