Skip to content

Wrong error reported when accessing an attribute #696

@aplavin

Description

@aplavin

Hi! I have a class that does some checking before setting an attribute value, like this (pure Python):

class A:
    def __setattr__(self, name, value):
        if value == 0:
            raise ValueError(value)
        # set value...

a = A()
a.a = 5  # OK
a.a = 0  # correct error message:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-7600552ca637> in <module>()
----> 1 a.a = 0

<ipython-input-6-585623315894> in __setattr__(self, name, value)
      2     def __setattr__(self, name, value):
      3         if value == 0:
----> 4             raise ValueError(value)
      5         # set value...
      6

ValueError: 0

However, running the same code with PyCall.jl gives a very confusing error message:

py"""
class A:
    def __setattr__(self, name, value):
        if value == 0:
            raise ValueError(value)
        # set value...
"""
a = py"A"()
a.a = 5  # OK
a.a = 0  # completely wrong exception:
ERROR: KeyError: key :a not found
Stacktrace:
 [1] setproperty!(::PyObject, ::Symbol, ::Int64) at /users/aplavin/.julia/packages/PyCall/ttONZ/src/PyCall.jl:329
 [2] top-level scope at none:0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions