Skip to content

Commit

Permalink
fix: Do not override value with loop variable
Browse files Browse the repository at this point in the history
- The enum conversion code uses value, which was also used
  as the name for a loop variable that iterates attributes.
  As a result, the loop variable overrode value, which is then
  checked if it is an enum. If the last attribute in a class
  instance happens to be an enum, the class instance will replace
  that enum value in the map.
  • Loading branch information
Christopher-Chianelli authored and triceo committed Sep 27, 2024
1 parent 6534b0f commit c416a8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/jpyinterpreter/src/main/python/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ def convert_object_to_java_python_like_object(value, instance_map=None):

if isinstance(out, AbstractPythonLikeObject):
try:
for (key, value) in object.__getattribute__(value, '__dict__').items():
out.setAttribute(key, convert_to_java_python_like_object(value, instance_map))
for (attribute_name, attribute_value) in object.__getattribute__(value, '__dict__').items():
out.setAttribute(attribute_name, convert_to_java_python_like_object(attribute_value, instance_map))
except AttributeError:
pass

Expand Down
22 changes: 22 additions & 0 deletions python/jpyinterpreter/tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,28 @@ def is_red(color: Color):
verifier.verify(Color.BLUE, expected_result=False)


def test_enum_as_attribute_in_class():
from enum import Enum
from dataclasses import dataclass

class Color(Enum):
RED = 'RED'
GREEN = 'GREEN'
BLUE = 'BLUE'

@dataclass
class Order:
color: Color

def is_red(order: Order):
return order.color is Color.RED

verifier = verifier_for(is_red)
verifier.verify(Order(Color.RED), expected_result=True)
verifier.verify(Order(Color.GREEN), expected_result=False)
verifier.verify(Order(Color.BLUE), expected_result=False)


def test_class_annotations():
from typing import Annotated
from java.lang import Deprecated
Expand Down

0 comments on commit c416a8b

Please sign in to comment.