prevent shadowing of computed vars (#3221)

This commit is contained in:
Thomas Brandého 2024-05-03 21:13:57 +02:00 committed by GitHub
parent 1817c30e22
commit a7355962fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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.