Skip to content

Store nested classes themselves on the outer class (#875) - #907

Closed
HoneyBearTech wants to merge 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-nested-class-attr
Closed

HoneyBearTech wants to merge 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-nested-class-attr

Conversation

@HoneyBearTech

Copy link
Copy Markdown
Contributor

Refs #875

What was broken
A class defined inside another class's body stayed on the outer class as the internal EvalLocalVar placeholder, not the class itself. So once the outer class was built, Outer.Inner behaved differently from Python:

class Outer:
    class Inner:
        pass

class Bar:
    y: Outer.Inner | None = None   # TypeError: unsupported operand type(s) for |: 'EvalLocalVar' and 'NoneType'

type(Outer.Inner()) is Outer.Inner  # False

This is the same error as #875, for a class nested in another class.

What I changed
Just before the outer class is created, each defined EvalLocalVar in its namespace is replaced by the value it holds. The placeholder objects themselves aren't changed, so methods that use the nested class by its bare name keep working as before. A class namespace that isn't a plain dict, such as one returned by a custom metaclass __prepare__, is left as it is.

Testing

  • New tests in tests/test_unit_eval.py, compared against CPython:
    • Outer.Inner | None, at module level and in another class's annotations;
    • type(Outer.Inner()) is Outer.Inner and isinstance;
    • a dataclass with a nested dataclass;
    • two levels of nesting.
  • Further tests:
    • one pins the bare-name method behaviour;
    • two check custom __prepare__ namespaces.
  • The full test suite, ruff, pylint, mypy and codespell pass on Python 3.14.

Note
#902 and #906 also change ast_classdef, in different lines. Whichever merges later may need a small rebase; I'm happy to do it.

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HoneyBearTech, another nice fix!

Could you rebase this onto current master? Only tests/test_unit_eval.py conflicts, because the #906 tests (added in 309a3ff) were also appended at the end of the file. eval.py applies cleanly on top of #902 and 0064c8c. I did a trial rebase: all 214 tests pass, and ruff, pylint and mypy are clean. Without the fix, all five CPython-comparison cases fail.

I also compared a few more cases with CPython. This fixes subclassing a nested class (class Sub(Outer.Inner) used to raise TypeError: EvalLocalVar.__init__()...), vars(Outer)['Inner'], self.Inner() in a method, and an outer class defined inside a function.

One small suggestion: test_nested_class_bare_name_in_method pins behavior that differs from Python (#902 lists it as a known limit). Could its docstring say that it documents the current behavior, so nobody later mistakes it for the intended one?

…#875)

A class defined inside a class body was left on the outer class as the
internal EvalLocalVar placeholder instead of the class itself. So after
the outer class was created, Outer.Inner | None raised the same
TypeError as in custom-components#875, and type(Outer.Inner()) is Outer.Inner was False.

Before the outer class is created, replace each defined EvalLocalVar in
a plain dict class namespace with the value it holds. The placeholders
themselves are left alone, so methods that read the nested class by its
bare name keep working as before. A class namespace that isn't a plain
dict, such as a mapping returned by a custom metaclass __prepare__, is
left as it is.

Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
@HoneyBearTech

Copy link
Copy Markdown
Contributor Author

@craigbarratt thanks for the review and the trial rebase! Rebased onto current master (now on top of #908 and #909 too). Only tests/test_unit_eval.py conflicted; I kept all the upstream tests as they are and put the nested-class tests after them. eval.py is unchanged from before.

I also updated the docstring of test_nested_class_bare_name_in_method: it now says the test pins current pyscript behavior that differs from Python (the known limit listed in #902), so it isn't mistaken for the intended behavior.

@craigbarratt

Copy link
Copy Markdown
Member

@HoneyBearTech - @dmamelin has a broader set of changes that include this PR in #915. Thanks for contributing this PR. Once #915 is merged, I'll close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants