prevent shadowing of computed vars (#3221)
This commit is contained in:
parent
1817c30e22
commit
a7355962fd
@ -450,6 +450,8 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
||||
super().__init_subclass__(**kwargs)
|
||||
# Event handlers should not shadow builtin state methods.
|
||||
cls._check_overridden_methods()
|
||||
# Computed vars should not shadow builtin state props.
|
||||
cls._check_overriden_basevars()
|
||||
|
||||
# Reset subclass tracking for this class.
|
||||
cls.class_subclasses = set()
|
||||
@ -696,6 +698,19 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
||||
f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _check_overriden_basevars(cls):
|
||||
"""Check for shadow base vars and raise error if any.
|
||||
|
||||
Raises:
|
||||
NameError: When a computed var shadows a base var.
|
||||
"""
|
||||
for computed_var_ in cls._get_computed_vars():
|
||||
if computed_var_._var_name in cls.__annotations__:
|
||||
raise NameError(
|
||||
f"The computed var name `{computed_var_._var_name}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_skip_vars(cls) -> set[str]:
|
||||
"""Get the vars to skip when serializing.
|
||||
|
Loading…
Reference in New Issue
Block a user