JuliaPy / JuliaPy/PyCall.jl

Misidentifying subclass of (python) list?

Abierto
#507 5 comentarios 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
Julia
Estrellas
1.5k
Forks
186
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I'm trying to wrap a package that defines a class that subtypes python's `list` and adds methods. When I create one of these, it appears to be misidentified as a `list`, so the additional methods are not available.

The offending line seems to be conversions.jl:762:
```
# only handle PyList for now
return pyisinstance(o, @pyglobalobj :PyList_Type) ? Array : Union{}
```
Would it break things to check if `type(o) == list` rather than whether it's an instance?

Example:
```
Main> py"class MyClass1(list): # subclass of list
def fn(self):
print 'waldo was here'
"

Main> py"class MyClass2(): # NOT a subclass of list
def fn(self):
print 'waldo was here'
"

Main> c1 = py"MyClass1()" # misidentified as a list => Array
0-element Array{Any,1}

Main> c2 = py"MyClass2()"
PyObject <__main__.MyClass2 instance at 0x1a48492758>

Main> py"$c1.fn()"
ERROR: PyError (ccall(@pysym(:PyEval_EvalCode), PyPtr, (PyPtr, PyPtr, PyPtr), o, globals, locals)) <
type 'exceptions.AttributeError'>
AttributeError("'list' object has no attribute 'fn'",)
File "/Users/rjp/.julia/v0.6/PyCall/src/pyeval.jl", line 1, in
const Py_single_input = 256 # from Python.h

Stacktrace:
[1] pyerr_check at /Users/rjp/.julia/v0.6/PyCall/src/exception.jl:60 [inlined]
[2] pyerr_check at /Users/rjp/.julia/v0.6/PyCall/src/exception.jl:64 [inlined]
[3] macro expansion at /Users/rjp/.julia/v0.6/PyCall/src/exception.jl:84 [inlined]
[4] pyeval_(::String, ::PyCall.PyDict{String,PyCall.PyObject,false}, ::PyCall.PyDict{String,PyCall.
PyObject,false}, ::Int64, ::String) at /Users/rjp/.julia/v0.6/PyCall/src/pyeval.jl:23
[5] eval(::Module, ::Any) at ./boot.jl:235

Main> py"$c2.fn()"
waldo was here
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

Start at conversions.jl:762 and reproduce the MyClass1/MyClass2 example from the issue in the Julia and Python environments. Trace how PyCall handles Python list subclasses and verify that the completed change preserves access to subclass methods while continuing to handle ordinary lists correctly.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
julia, python
Área
backend
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.