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
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.
The text was updated successfully, but these errors were encountered:
da-woods
added a commit
to da-woods/cython
that referenced
this issue
Dec 26, 2020
Fixescython#3954.
The problem seems to be that __Pyx_CallUnboundCMethod0 optimizes
the call incorrectly. This patch avoids it getting sent to
that mechanism.
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?
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 pythonicRemoving 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.
The text was updated successfully, but these errors were encountered: