|
188 | 188 | WithStmt, |
189 | 189 | YieldExpr, |
190 | 190 | YieldFromExpr, |
| 191 | + func_scoped_name, |
191 | 192 | get_member_expr_fullname, |
192 | 193 | implicit_module_attrs, |
| 194 | + inline_base, |
193 | 195 | is_final_node, |
194 | 196 | type_aliases, |
195 | 197 | type_aliases_source_versions, |
@@ -1992,7 +1994,7 @@ def analyze_class(self, defn: ClassDef) -> None: |
1992 | 1994 | return |
1993 | 1995 |
|
1994 | 1996 | self.analyze_class_keywords(defn) |
1995 | | - bases_result = self.analyze_base_classes(bases) |
| 1997 | + bases_result = self.analyze_base_classes(defn.name, bases) |
1996 | 1998 | if bases_result is None or self.found_incomplete_ref(tag): |
1997 | 1999 | # Something was incomplete. Defer current target. |
1998 | 2000 | self.mark_incomplete(defn.name, defn) |
@@ -2112,7 +2114,7 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool: |
2112 | 2114 | if info is None: |
2113 | 2115 | self.mark_incomplete(defn.name, defn) |
2114 | 2116 | else: |
2115 | | - self.prepare_class_def(defn, info, custom_names=True) |
| 2117 | + self.prepare_class_def(defn, info) |
2116 | 2118 | for decorator in defn.decorators: |
2117 | 2119 | decorator.accept(self) |
2118 | 2120 | if defn.info: |
@@ -2136,13 +2138,13 @@ def analyze_namedtuple_classdef( |
2136 | 2138 | info: TypeInfo | None = defn.info |
2137 | 2139 | else: |
2138 | 2140 | is_named_tuple, info = self.named_tuple_analyzer.analyze_namedtuple_classdef( |
2139 | | - defn, self.is_stub_file, self.is_func_scope() |
| 2141 | + defn, self.is_stub_file |
2140 | 2142 | ) |
2141 | 2143 | if is_named_tuple: |
2142 | 2144 | if info is None: |
2143 | 2145 | self.mark_incomplete(defn.name, defn) |
2144 | 2146 | else: |
2145 | | - self.prepare_class_def(defn, info, custom_names=True) |
| 2147 | + self.prepare_class_def(defn, info) |
2146 | 2148 | self.setup_type_vars(defn, tvar_defs) |
2147 | 2149 | self.setup_alias_type_vars(defn) |
2148 | 2150 | with self.scope.class_scope(defn.info): |
@@ -2512,51 +2514,26 @@ def get_and_bind_all_tvars(self, type_exprs: list[Expression]) -> list[TypeVarLi |
2512 | 2514 | tvar_defs.append(tvar_def) |
2513 | 2515 | return tvar_defs |
2514 | 2516 |
|
2515 | | - def prepare_class_def( |
2516 | | - self, defn: ClassDef, info: TypeInfo | None = None, custom_names: bool = False |
2517 | | - ) -> None: |
| 2517 | + def class_fullname(self, name: str, line: int) -> str: |
| 2518 | + if not self.is_nested_within_func_scope(): |
| 2519 | + return self.qualified_name(name) |
| 2520 | + name = func_scoped_name(name, line) |
| 2521 | + return f"{self.cur_mod_id}.{name}" |
| 2522 | + |
| 2523 | + def prepare_class_def(self, defn: ClassDef, info: TypeInfo | None = None) -> None: |
2518 | 2524 | """Prepare for the analysis of a class definition. |
2519 | 2525 |
|
2520 | 2526 | Create an empty TypeInfo and store it in a symbol table, or if the 'info' |
2521 | 2527 | argument is provided, store it instead (used for magic type definitions). |
2522 | 2528 | """ |
2523 | 2529 | if not defn.info: |
2524 | | - defn.fullname = self.qualified_name(defn.name) |
2525 | | - # TODO: Nested classes |
| 2530 | + defn.fullname = self.class_fullname(defn.name, defn.line) |
2526 | 2531 | info = info or self.make_empty_type_info(defn) |
2527 | 2532 | defn.info = info |
2528 | 2533 | info.defn = defn |
2529 | | - if not custom_names: |
2530 | | - # Some special classes (in particular NamedTuples) use custom fullname logic. |
2531 | | - # Don't override it here (also see comment below, this needs cleanup). |
2532 | | - if not self.is_func_scope(): |
2533 | | - info._fullname = self.qualified_name(defn.name) |
2534 | | - else: |
2535 | | - info._fullname = info.name |
2536 | | - local_name = defn.name |
2537 | | - if "@" in local_name: |
2538 | | - local_name = local_name.split("@")[0] |
2539 | | - self.add_symbol(local_name, defn.info, defn) |
| 2534 | + self.add_symbol(defn.name, defn.info, defn) |
2540 | 2535 | if self.is_nested_within_func_scope(): |
2541 | | - # We need to preserve local classes, let's store them |
2542 | | - # in globals under mangled unique names |
2543 | | - # |
2544 | | - # TODO: Putting local classes into globals breaks assumptions in fine-grained |
2545 | | - # incremental mode and we should avoid it. In general, this logic is too |
2546 | | - # ad-hoc and needs to be removed/refactored. |
2547 | | - if "@" not in defn.info._fullname: |
2548 | | - global_name = defn.info.name + "@" + str(defn.line) |
2549 | | - defn.info._fullname = self.cur_mod_id + "." + global_name |
2550 | | - else: |
2551 | | - # Preserve name from previous fine-grained incremental run. |
2552 | | - global_name = defn.info.name |
2553 | | - defn.fullname = defn.info._fullname |
2554 | | - if defn.info.is_named_tuple or defn.info.typeddict_type: |
2555 | | - # Named tuples and Typed dicts nested within a class are stored |
2556 | | - # in the class symbol table. |
2557 | | - self.add_symbol_skip_local(global_name, defn.info) |
2558 | | - else: |
2559 | | - self.globals[global_name] = SymbolTableNode(GDEF, defn.info) |
| 2536 | + self.add_global_symbol(defn.name, defn, defn.info) |
2560 | 2537 |
|
2561 | 2538 | def make_empty_type_info(self, defn: ClassDef) -> TypeInfo: |
2562 | 2539 | if ( |
@@ -2587,7 +2564,7 @@ def get_name_repr_of_expr(self, expr: Expression) -> str | None: |
2587 | 2564 | return None |
2588 | 2565 |
|
2589 | 2566 | def analyze_base_classes( |
2590 | | - self, base_type_exprs: list[Expression] |
| 2567 | + self, cls_name: str, base_type_exprs: list[Expression] |
2591 | 2568 | ) -> tuple[list[tuple[ProperType, Expression]], bool] | None: |
2592 | 2569 | """Analyze base class types. |
2593 | 2570 |
|
@@ -2599,7 +2576,7 @@ def analyze_base_classes( |
2599 | 2576 | """ |
2600 | 2577 | is_error = False |
2601 | 2578 | bases = [] |
2602 | | - for base_expr in base_type_exprs: |
| 2579 | + for i, base_expr in enumerate(base_type_exprs): |
2603 | 2580 | if ( |
2604 | 2581 | isinstance(base_expr, RefExpr) |
2605 | 2582 | and base_expr.fullname in TYPED_NAMEDTUPLE_NAMES + TPDICT_NAMES |
@@ -2617,7 +2594,10 @@ def analyze_base_classes( |
2617 | 2594 |
|
2618 | 2595 | try: |
2619 | 2596 | base = self.expr_to_analyzed_type( |
2620 | | - base_expr, allow_placeholder=True, allow_type_any=True |
| 2597 | + base_expr, |
| 2598 | + allow_placeholder=True, |
| 2599 | + allow_type_any=True, |
| 2600 | + unique_name=inline_base(cls_name, i), |
2621 | 2601 | ) |
2622 | 2602 | except TypeTranslationError: |
2623 | 2603 | name = self.get_name_repr_of_expr(base_expr) |
@@ -3594,7 +3574,7 @@ def analyze_enum_assign(self, s: AssignmentStmt) -> bool: |
3594 | 3574 | # This is an analyzed enum definition. |
3595 | 3575 | # It is valid iff it can be stored correctly, failures were already reported. |
3596 | 3576 | return self._is_single_name_assignment(s) |
3597 | | - return self.enum_call_analyzer.process_enum_call(s, self.is_func_scope()) |
| 3577 | + return self.enum_call_analyzer.process_enum_call(s) |
3598 | 3578 |
|
3599 | 3579 | def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool: |
3600 | 3580 | """Check if s defines a namedtuple.""" |
@@ -3618,7 +3598,7 @@ def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool: |
3618 | 3598 | namespace = self.qualified_name(name) |
3619 | 3599 | with self.tvar_scope_frame(self.tvar_scope.class_frame(namespace)): |
3620 | 3600 | internal_name, info, tvar_defs = self.named_tuple_analyzer.check_namedtuple( |
3621 | | - s.rvalue, name, self.is_func_scope() |
| 3601 | + s.rvalue, name |
3622 | 3602 | ) |
3623 | 3603 | if internal_name is None: |
3624 | 3604 | return False |
@@ -3655,7 +3635,7 @@ def analyze_typeddict_assign(self, s: AssignmentStmt) -> bool: |
3655 | 3635 | namespace = self.qualified_name(name) |
3656 | 3636 | with self.tvar_scope_frame(self.tvar_scope.class_frame(namespace)): |
3657 | 3637 | is_typed_dict, info, tvar_defs = self.typed_dict_analyzer.check_typeddict( |
3658 | | - s.rvalue, name, self.is_func_scope() |
| 3638 | + s.rvalue, name |
3659 | 3639 | ) |
3660 | 3640 | if not is_typed_dict: |
3661 | 3641 | return False |
@@ -5161,17 +5141,18 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool: |
5161 | 5141 | return True |
5162 | 5142 |
|
5163 | 5143 | def basic_new_typeinfo(self, name: str, basetype_or_fallback: Instance, line: int) -> TypeInfo: |
5164 | | - if self.is_func_scope() and not self.type and "@" not in name: |
5165 | | - name += "@" + str(line) |
5166 | 5144 | class_def = ClassDef(name, Block([])) |
5167 | | - if self.is_func_scope() and not self.type: |
5168 | | - # Full names of generated classes should always be prefixed with the module names |
5169 | | - # even if they are nested in a function, since these classes will be (de-)serialized. |
5170 | | - # (Note that the caller should append @line to the name to avoid collisions.) |
5171 | | - # TODO: clean this up, see #6422. |
5172 | | - class_def.fullname = self.cur_mod_id + "." + self.qualified_name(name) |
5173 | | - else: |
5174 | | - class_def.fullname = self.qualified_name(name) |
| 5145 | + # Ground rules for classes nested in functions: |
| 5146 | + # * Use is_nested_within_func_scope(), not is_func_scope(), to determine whether |
| 5147 | + # to use any special logic, because nothing inside top-level functions is serialized. |
| 5148 | + # * ClassDef.name is not mangled (i.e. @line suffix is not appended). |
| 5149 | + # * ClassDef.fullname, and thus TypeInfo.fullname are always pkg.mod.Name@line, any |
| 5150 | + # "intermediate" classes are not included in the fullname. |
| 5151 | + # * The caller is responsible for storing the generated TypeInfo twice: once as usual |
| 5152 | + # with add_symbol(), and once using add_global_symbol() using the mangled name. |
| 5153 | + # The second one is needed to properly serialize any classes nested in functions. |
| 5154 | + # TODO: make sure the daemon works well with these rules. |
| 5155 | + class_def.fullname = self.class_fullname(name, line) |
5175 | 5156 |
|
5176 | 5157 | info = TypeInfo(SymbolTable(), class_def, self.cur_mod_id) |
5177 | 5158 | class_def.info = info |
@@ -7030,27 +7011,18 @@ def add_symbol( |
7030 | 7011 | name, symbol, context, can_defer, escape_comprehensions, no_progress, type_param |
7031 | 7012 | ) |
7032 | 7013 |
|
7033 | | - def add_symbol_skip_local(self, name: str, node: SymbolNode) -> None: |
7034 | | - """Same as above, but skipping the local namespace. |
| 7014 | + def add_global_symbol(self, name: str, ctx: Context, node: SymbolNode) -> None: |
| 7015 | + """Add symbol to a global namespace. |
7035 | 7016 |
|
7036 | 7017 | This doesn't check for previous definition and is only used |
7037 | | - for serialization of method-level classes. |
| 7018 | + for serialization of classes nested in functions/methods. |
7038 | 7019 |
|
7039 | 7020 | Classes defined within methods can be exposed through an |
7040 | 7021 | attribute type, but method-level symbol tables aren't serialized. |
7041 | 7022 | This method can be used to add such classes to an enclosing, |
7042 | 7023 | serialized symbol table. |
7043 | 7024 | """ |
7044 | | - # TODO: currently this is only used by named tuples and typed dicts. |
7045 | | - # Use this method also by normal classes, see issue #6422. |
7046 | | - if self.type is not None: |
7047 | | - names = self.type.names |
7048 | | - kind = MDEF |
7049 | | - else: |
7050 | | - names = self.globals |
7051 | | - kind = GDEF |
7052 | | - symbol = SymbolTableNode(kind, node) |
7053 | | - names[name] = symbol |
| 7025 | + self.globals[func_scoped_name(name, ctx.line)] = SymbolTableNode(GDEF, node) |
7054 | 7026 |
|
7055 | 7027 | def add_symbol_table_node( |
7056 | 7028 | self, |
@@ -7111,8 +7083,10 @@ def add_symbol_table_node( |
7111 | 7083 | if isinstance(old, Var) and is_init_only(old): |
7112 | 7084 | if old.has_explicit_value: |
7113 | 7085 | self.fail("InitVar with default value cannot be redefined", context) |
7114 | | - elif not ( |
7115 | | - isinstance(new, (FuncDef, Decorator)) and self.set_original_def(old, new) |
| 7086 | + elif ( |
| 7087 | + not (isinstance(new, (FuncDef, Decorator)) and self.set_original_def(old, new)) |
| 7088 | + # Avoid (additional) errors for internal symbols. |
| 7089 | + and "@" not in name |
7116 | 7090 | ): |
7117 | 7091 | self.name_already_defined(name, context, existing) |
7118 | 7092 | elif type_param or ( |
@@ -7710,14 +7684,15 @@ def expr_to_analyzed_type( |
7710 | 7684 | allow_unbound_tvars: bool = False, |
7711 | 7685 | allow_param_spec_literals: bool = False, |
7712 | 7686 | allow_unpack: bool = False, |
| 7687 | + unique_name: str | None = None, |
7713 | 7688 | ) -> Type | None: |
7714 | | - if isinstance(expr, CallExpr): |
| 7689 | + if unique_name is not None and isinstance(expr, CallExpr): |
7715 | 7690 | # This is a legacy syntax intended mostly for Python 2, we keep it for |
7716 | 7691 | # backwards compatibility, but new features like generic named tuples |
7717 | 7692 | # and recursive named tuples will be not supported. |
7718 | 7693 | expr.accept(self) |
7719 | 7694 | internal_name, info, tvar_defs = self.named_tuple_analyzer.check_namedtuple( |
7720 | | - expr, None, self.is_func_scope() |
| 7695 | + expr, unique_name |
7721 | 7696 | ) |
7722 | 7697 | if tvar_defs: |
7723 | 7698 | self.fail("Generic named tuples are not supported for legacy class syntax", expr) |
|