Skip to content

Commit

Permalink
Process NamedTuple decorators in semantic analyzer (#15513)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 committed Jun 28, 2023
1 parent fca4cae commit 9a4a5aa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,10 @@ def analyze_namedtuple_classdef(
self.setup_type_vars(defn, tvar_defs)
self.setup_alias_type_vars(defn)
with self.scope.class_scope(defn.info):
for deco in defn.decorators:
deco.accept(self)
if isinstance(deco, RefExpr) and deco.fullname in FINAL_DECORATOR_NAMES:
info.is_final = True
with self.named_tuple_analyzer.save_namedtuple_body(info):
self.analyze_class_body_common(defn)
return True
Expand Down
6 changes: 6 additions & 0 deletions mypyc/test-data/run-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ assert f(Sub(3, 2)) == 3
-- Ref: https://github.com/mypyc/mypyc/issues/924
[case testNamedTupleClassSyntax]
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
from typing_extensions import final

class FuncIR: pass

Expand All @@ -121,6 +122,11 @@ class Record(NamedTuple):
# Ref: https://github.com/mypyc/mypyc/issues/938
class ClassIR: pass

# Ref: https://github.com/mypyc/mypyc/issues/927
@final
class Inextensible(NamedTuple):
x: int

[file driver.py]
from typing import ForwardRef, Optional
from native import ClassIR, FuncIR, Record
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/semanal-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,23 @@ class B(A): pass
[out]
main:2: error: Unsupported dynamic base class "NamedTuple"
main:2: error: Name "NamedTuple" is not defined

[case testNamedTupleWithDecorator]
from typing import final, NamedTuple

@final
class A(NamedTuple("N", [("x", int)])):
pass
[builtins fixtures/tuple.pyi]
[out]
MypyFile:1(
ImportFrom:1(typing, [final, NamedTuple])
ClassDef:4(
A
TupleType(
Tuple[builtins.int, fallback=__main__.N@4])
Decorators(
NameExpr(final [typing.final]))
BaseType(
__main__.N@4)
PassStmt:5()))

0 comments on commit 9a4a5aa

Please sign in to comment.