Skip to content

[Bug]: __repr__ TypeError reports wrong type in error message #676

@jseop-lim

Description

@jseop-lim

Description

When __repr__ returns a non-string value, the TypeError message incorrectly reports the type of the object being repr'd, instead of the type of the invalid return value. This is a CPython compatibility issue.

Root Cause

In PyObjectReprAsObjectNode.java#L111, raiseTypeError is called with obj (the object being repr'd) instead of result (the return value of __repr__), so the error message prints the wrong type name. Note that the equivalent __str__ path in PyObjectStrAsObjectNode.java#L128 already correctly passes result.

CPython reference: Objects/object.c#L579-L581

Fix

Pass the __repr__ return value (result) instead of the original object (obj) to raiseTypeError, matching CPython's behavior.

Reproduction

class A:
    def __repr__(self):
        return True

repr(A())

Output

GraalPy:

Traceback (most recent call last):
  File "/tmp/test.py", line 5, in <module>
    repr(A())
TypeError: __repr__ returned non-string (type A)

CPython:

Traceback (most recent call last):
  File "/tmp/test.py", line 5, in <module>
    repr(A())
TypeError: __repr__ returned non-string (type NoneType)

Environment

  • GraalPy 25.0.2 (Python 3.12.8)
  • CPython 3.12.13
  • OS: Debian 12
  • CPU: arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions