Skip to content
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

Process NamedTuple decorators in semantic analyzer #15513

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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()))