Skip to content

Commit

Permalink
[mypyc] fix setup conflict with attributes named up (#13012)
Browse files Browse the repository at this point in the history
  • Loading branch information
davfsa committed Jul 17, 2022
1 parent 8e609b2 commit 3e978b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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"

0 comments on commit 3e978b2

Please sign in to comment.