Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
554e393
Add failing test cases
JukkaL Dec 18, 2022
96b2767
Fix accessing attribute that overrides a property
JukkaL Dec 18, 2022
f8f65cd
WIP partially enable test case
JukkaL Dec 18, 2022
c9f9ae8
Refactor mypyc.irbuild.prepare a little
JukkaL Dec 18, 2022
147ccde
More refactoring
JukkaL Dec 18, 2022
e507645
Prefer attributes over methods, if both are defined
JukkaL Dec 18, 2022
0243a1e
Generate method decl for implicit property getters
JukkaL Dec 18, 2022
15b7b30
Fix serialization of the "implicit" attribute
JukkaL Dec 18, 2022
8e638b0
Support simple use cases where an attribute overrides a property
JukkaL Dec 18, 2022
44075a9
Fix case where both attribute and property are inherited
JukkaL Dec 18, 2022
c756c36
Support settable properties
JukkaL Dec 30, 2022
de7e436
Black + isort
JukkaL Dec 30, 2022
d92905d
Merge test cases
JukkaL Dec 30, 2022
1fd4bc5
Merge more test cases
JukkaL Dec 30, 2022
d80842a
Fix edge case
JukkaL Dec 30, 2022
0274969
Test case fixes
JukkaL Dec 30, 2022
810c4e1
Add native int test cases
JukkaL Dec 30, 2022
b0ee424
Fix test case
JukkaL Jan 2, 2023
479ffcd
Minor refactoring
JukkaL Jan 2, 2023
039fe10
Fix self check
JukkaL Jan 2, 2023
1996b8a
Improve comments and minor cleanup
JukkaL Jan 2, 2023
f5d4560
Minor refactoring
JukkaL Jan 2, 2023
8fc012e
Make test cases compatible with CPython
JukkaL Jan 10, 2023
d948ce8
Merge branch 'master' into mypyc-property-inheritance
JukkaL Jan 10, 2023
158ee54
Merge remote-tracking branch 'origin/master' into mypyc-property-inhe…
JukkaL Jan 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor mypyc.irbuild.prepare a little
  • Loading branch information
JukkaL committed Dec 30, 2022
commit c9f9ae82b7e68e5486273d2adca981cc0d15cac6
67 changes: 40 additions & 27 deletions mypyc/irbuild/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ def can_subclass_builtin(builtin_base: str) -> bool:
def prepare_class_def(
path: str, module_name: str, cdef: ClassDef, errors: Errors, mapper: Mapper
) -> None:
"""Populate the interface-level information in a class IR.

This includes attribute and method declarations, and the MRO, among other things, but
method bodies are generated in a later pass.
"""

ir = mapper.type_to_ir[cdef.info]
info = cdef.info
Expand All @@ -223,31 +228,7 @@ def prepare_class_def(
# Supports copy.copy and pickle (including subclasses)
ir._serializable = True

# We sort the table for determinism here on Python 3.5
for name, node in sorted(info.names.items()):
# Currently all plugin generated methods are dummies and not included.
if node.plugin_generated:
continue

if isinstance(node.node, Var):
assert node.node.type, "Class member %s missing type" % name
if not node.node.is_classvar and name not in ("__slots__", "__deletable__"):
ir.attributes[name] = mapper.type_to_rtype(node.node.type)
elif isinstance(node.node, (FuncDef, Decorator)):
prepare_method_def(ir, module_name, cdef, mapper, node.node)
elif isinstance(node.node, OverloadedFuncDef):
# Handle case for property with both a getter and a setter
if node.node.is_property:
if is_valid_multipart_property_def(node.node):
for item in node.node.items:
prepare_method_def(ir, module_name, cdef, mapper, item)
else:
errors.error("Unsupported property decorator semantics", path, cdef.line)

# Handle case for regular function overload
else:
assert node.node.impl
prepare_method_def(ir, module_name, cdef, mapper, node.node.impl)
populate_methods_and_attributes(cdef, ir, path, module_name, errors, mapper)

# Check for subclassing from builtin types
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I had to restructure this to do things in a different order. Most of the logic hasn't changed. I also extracted some functionality to separate methods since the function was already quite big.

for cls in info.mro:
Expand Down Expand Up @@ -304,8 +285,8 @@ def prepare_class_def(
errors.error("Non-trait bases must appear first in parent list", path, cdef.line)
ir.traits = [c for c in bases if c.is_trait]

mro = []
base_mro = []
mro = [] # All mypyc base classes
base_mro = [] # Non-trait mypyc base classes
for cls in info.mro:
if cls not in mapper.type_to_ir:
if cls.fullname != "builtins.object":
Expand Down Expand Up @@ -333,6 +314,38 @@ def prepare_class_def(
ir.is_augmented = True


def populate_methods_and_attributes(
cdef: ClassDef, ir: ClassIR, path: str, module_name: str, errors: Errors, mapper: Mapper
) -> None:
"""Populate attribute and method declarations."""
info = cdef.info
# We sort the table for determinism here on Python 3.5.
for name, node in sorted(info.names.items()):
# Currently all plugin generated methods are dummies and not included.
if node.plugin_generated:
continue

if isinstance(node.node, Var):
assert node.node.type, "Class member %s missing type" % name
if not node.node.is_classvar and name not in ("__slots__", "__deletable__"):
ir.attributes[name] = mapper.type_to_rtype(node.node.type)
elif isinstance(node.node, (FuncDef, Decorator)):
prepare_method_def(ir, module_name, cdef, mapper, node.node)
elif isinstance(node.node, OverloadedFuncDef):
# Handle case for property with both a getter and a setter
if node.node.is_property:
if is_valid_multipart_property_def(node.node):
for item in node.node.items:
prepare_method_def(ir, module_name, cdef, mapper, item)
else:
errors.error("Unsupported property decorator semantics", path, cdef.line)

# Handle case for regular function overload
else:
assert node.node.impl
prepare_method_def(ir, module_name, cdef, mapper, node.node.impl)


def prepare_non_ext_class_def(
path: str, module_name: str, cdef: ClassDef, errors: Errors, mapper: Mapper
) -> None:
Expand Down