MCPcopy Index your code
hub / github.com/python/mypy / add_attribute_to_class

Function add_attribute_to_class

mypy/plugins/common.py:391–433  ·  view source on GitHub ↗

Adds a new attribute to a class definition. This currently only generates the symbol table entry and no corresponding AssignmentStatement

(
    api: SemanticAnalyzerPluginInterface,
    cls: ClassDef,
    name: str,
    typ: Type,
    final: bool = False,
    no_serialize: bool = False,
    override_allow_incompatible: bool = False,
    fullname: str | None = None,
    is_classvar: bool = False,
    overwrite_existing: bool = False,
)

Source from the content-addressed store, hash-verified

389
390
391def add_attribute_to_class(
392 api: SemanticAnalyzerPluginInterface,
393 cls: ClassDef,
394 name: str,
395 typ: Type,
396 final: bool = False,
397 no_serialize: bool = False,
398 override_allow_incompatible: bool = False,
399 fullname: str | None = None,
400 is_classvar: bool = False,
401 overwrite_existing: bool = False,
402) -> Var:
403 """
404 Adds a new attribute to a class definition.
405 This currently only generates the symbol table entry and no corresponding AssignmentStatement
406 """
407 info = cls.info
408
409 # NOTE: we would like the plugin generated node to dominate, but we still
410 # need to keep any existing definitions so they get semantically analyzed.
411 if name in info.names and not overwrite_existing:
412 # Get a nice unique name instead.
413 r_name = get_unique_redefinition_name(name, info.names)
414 info.names[r_name] = info.names[name]
415
416 node = Var(name, typ)
417 node.info = info
418 node.is_final = final
419 node.is_classvar = is_classvar
420 if name in ALLOW_INCOMPATIBLE_OVERRIDE:
421 node.allow_incompatible_override = True
422 else:
423 node.allow_incompatible_override = override_allow_incompatible
424
425 if fullname:
426 node._fullname = fullname
427 else:
428 node._fullname = info.fullname + "." + name
429
430 info.names[name] = SymbolTableNode(
431 MDEF, node, plugin_generated=True, no_serialize=no_serialize
432 )
433 return node
434
435
436# We keep the unused `api` parameter, to avoid breaking 3rd party dataclass-like plugins.

Callers 6

transformMethod · 0.90
add_slotsMethod · 0.90
_add_slotsFunction · 0.90
_add_match_argsFunction · 0.90
_remove_hashabilityFunction · 0.90

Calls 3

VarClass · 0.90
SymbolTableNodeClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…