Skip to content

[mypyc] Fix setup conflict when setting an attribute with name up #13012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions mypyc/codegen/emitclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ def emit_line() -> None:


def getter_name(cl: ClassIR, attribute: str, names: NameGenerator) -> str:
return names.private_name(cl.module_name, f'{cl.name}_get{attribute}')
return names.private_name(cl.module_name, f'{cl.name}_get_{attribute}')


def setter_name(cl: ClassIR, attribute: str, names: NameGenerator) -> str:
return names.private_name(cl.module_name, f'{cl.name}_set{attribute}')
return names.private_name(cl.module_name, f'{cl.name}_set_{attribute}')


def generate_object_struct(cl: ClassIR, emitter: Emitter) -> None:
Expand Down
17 changes: 16 additions & 1 deletion mypyc/test/test_emitclass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest

from mypyc.codegen.emitclass import slot_key
from mypyc.codegen.emitclass import getter_name, setter_name, slot_key
from mypyc.ir.class_ir import ClassIR
from mypyc.namegen import NameGenerator


class TestEmitClass(unittest.TestCase):
Expand All @@ -10,3 +12,16 @@ def test_slot_key(self) -> None:
# __delitem__ and reverse methods should come last.
assert s == [
'__add__', '__rshift__', '__setitem__', '__delitem__', '__radd__', '__rrshift__']

def test_setter_name(self) -> None:
cls = ClassIR(module_name="testing", name="SomeClass")
generator = NameGenerator([['mod']])

# This should never be `setup`, as it will conflict with the class `setup`
assert setter_name(cls, "up", generator) == "testing___SomeClass_set_up"

def test_getter_name(self) -> None:
cls = ClassIR(module_name="testing", name="SomeClass")
generator = NameGenerator([['mod']])

assert getter_name(cls, "down", generator) == "testing___SomeClass_get_down"