You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unexpected behavior of __class__ attribute for annotated python arguments #3954
Hello, I'm kinda new to Cython so hopefully I didn't miss anything obvious, but I think I've found a weird behavior of annotated function arguments.
Seems like the __class__ attribute acts unexpectedly when the arguments are annotated with pythonic
deffunc(dict my_dict):
# The following prints "<class 'dict'>" as expectedprint(my_dict.__class__)
# I would expect the following to print "{}" since this is the equivalent of dict()# Instead, it once again prints "<class 'dict'>print(my_dict.__class__())
# Calling the class twice would create an empty dict and print "{}"print(my_dict.__class__()())
Removing the "dict" annotation from "my_dict" would make this function work as expected
(printing "<class 'dict'> followed by "{}" followed by an exception for calling a 'dict' object)
This behavior also occurs with other python types such as strings, lists and sets.
However, when using ints, it behaved has expected.
I'm using Cython 0.29.21 with Python 3.9.1 on Windows.
Agreed that this is a bug, but is there any use case for such code? I can't think of a case where [some builtin object].__class__ isn't just the builtin type itself.
Agreed that this is a bug, but is there any use case for such code? I can't think of a case where [some builtin object].__class__ isn't just the builtin type itself.
This is very artificial, but you can come up with cases where the user hasn't actually typed a variable, but Cython has successfully inferred it:
def f(x: list):
y = x*2 # hmmmm - what type is this going to be?
return y.__class__()
Here Cython works out that y is going to be a list but it isn't necessarily obvious to the user. I haven't really convinced myself that this is a use-case, but you could invent more complicated examples: maybe y was created from a cdef function with a specified return type?
Activity
Fixed __class__() lookup on builtin types
scoder commentedon Mar 21, 2021
Agreed that this is a bug, but is there any use case for such code? I can't think of a case where
[some builtin object].__class__isn't just the builtin type itself.da-woods commentedon Mar 21, 2021
This is very artificial, but you can come up with cases where the user hasn't actually typed a variable, but Cython has successfully inferred it:
Here Cython works out that
yis going to be alistbut it isn't necessarily obvious to the user. I haven't really convinced myself that this is a use-case, but you could invent more complicated examples: maybeywas created from a cdef function with a specified return type?Fix .__class__() calls on builtin types (GH-3956)