Repository navigation
Store nested classes themselves on the outer class (#875) - #907
HoneyBearTech wants to merge 1 commit into
Conversation
craigbarratt
left a comment
There was a problem hiding this comment.
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 <[email protected]>
3bf8f9a to
9d5e904
Compare
|
@craigbarratt thanks for the review and the trial rebase! Rebased onto current master (now on top of #908 and #909 too). Only I also updated the docstring of |
|
@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. |
Refs #875
What was broken
A class defined inside another class's body stayed on the outer class as the internal
EvalLocalVarplaceholder, not the class itself. So once the outer class was built,Outer.Innerbehaved differently from Python: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
EvalLocalVarin 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 plaindict, such as one returned by a custom metaclass__prepare__, is left as it is.Testing
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.Innerandisinstance;__prepare__namespaces.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.