Skip to content

Commit e1e220c

Browse files
ilevkivskyiIvan Levkivskyi
andcommitted
Clear type alias flag for assignments that are not aliases (#11446)
This flag may be set to `True` if there are tricky forward references. Co-authored-by: Ivan Levkivskyi <[email protected]>
1 parent bcbc901 commit e1e220c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

mypy/semanal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,9 +2032,13 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
20322032
special_form = True
20332033
elif self.analyze_enum_assign(s):
20342034
special_form = True
2035+
20352036
if special_form:
20362037
self.record_special_form_lvalue(s)
20372038
return
2039+
# Clear the alias flag if assignment turns out not a special form after all. It
2040+
# may be set to True while there were still placeholders due to forward refs.
2041+
s.is_alias_def = False
20382042

20392043
# OK, this is a regular assignment, perform the necessary analysis steps.
20402044
s.is_final_def = self.unwrap_final(s)

test-data/unit/check-callable.test

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,56 @@ reveal_type(_TYPE) # N: Revealed type is "def (x: Any) -> builtins.type"
484484
_TYPE('bar')
485485

486486
[builtins fixtures/callable.pyi]
487+
488+
[case testErrorMessageAboutSelf]
489+
# https://github.com/python/mypy/issues/11309
490+
class Some:
491+
def method(self, a) -> None: pass
492+
@classmethod
493+
def cls_method(cls, a) -> None: pass
494+
@staticmethod
495+
def st_method(a) -> None: pass
496+
497+
def bad_method(a) -> None: pass
498+
@classmethod
499+
def bad_cls_method(a) -> None: pass
500+
@staticmethod
501+
def bad_st_method() -> None: pass
502+
503+
s: Some
504+
505+
s.method(1)
506+
s.cls_method(1)
507+
Some.cls_method(1)
508+
s.st_method(1)
509+
Some.st_method(1)
510+
511+
s.method(1, 2) # E: Too many arguments for "method" of "Some"
512+
s.cls_method(1, 2) # E: Too many arguments for "cls_method" of "Some"
513+
Some.cls_method(1, 2) # E: Too many arguments for "cls_method" of "Some"
514+
s.st_method(1, 2) # E: Too many arguments for "st_method" of "Some"
515+
Some.st_method(1, 2) # E: Too many arguments for "st_method" of "Some"
516+
517+
s.bad_method(1) # E: Too many arguments for "bad_method" of "Some" \
518+
# N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
519+
s.bad_cls_method(1) # E: Too many arguments for "bad_cls_method" of "Some" \
520+
# N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
521+
Some.bad_cls_method(1) # E: Too many arguments for "bad_cls_method" of "Some" \
522+
# N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
523+
s.bad_st_method(1) # E: Too many arguments for "bad_st_method" of "Some"
524+
Some.bad_st_method(1) # E: Too many arguments for "bad_st_method" of "Some"
525+
[builtins fixtures/callable.pyi]
526+
527+
[case testClassMethodAliasStub]
528+
from a import f
529+
f("no") # E: Argument 1 has incompatible type "str"; expected "int"
530+
[file a.pyi]
531+
from b import C
532+
f = C.f
533+
[file b.pyi]
534+
import a
535+
class C(B):
536+
@classmethod
537+
def f(self, x: int) -> C: ...
538+
class B: ...
539+
[builtins fixtures/classmethod.pyi]

0 commit comments

Comments
 (0)