Alpha7 Pure python code gives : TypeError: Expected unicode, got FieldInfo

396 views
Skip to first unread message

Steven Johnson

unread,
May 27, 2021, 12:32:48 AM5/27/21
to cython-users

Basically it seems that every python file i compile in my project crashes with the error:
"TypeError: Expected unicode, got FieldInfo"

The problem seems to be because i use Pydantic and have classes defined like this:

class ServiceRow(BaseModel):
"""An individual record."""

    service_id: int = Field(..., description="The ID for the service.", alias="id")
    name: str = Field(..., description="Displayable Name for the service.")

So, the class level "name" property is not really a string,  Pydantic uses the type hint to know what type the instantiated property "name" should be and it uses the Field assignment to give all sorts of metadata to that property for documentation and validation purposes.


How do i deal with this and tell Cython that at the class level this is a FieldInfo and not a string, but in the instance, its a string?

D Woods

unread,
May 27, 2021, 2:31:48 AM5/27/21
to cython-users
Possibly turn annotation_typing off (`#cython: annotation_typing=False`)? You can only do it at the module level.

However, I'm not sure Cython understands the idea that you've set an attribute at both the class level and the instance level in a cdef class. This suggests to me that your data isn't a good fit for a cdef class and you should leave it as a regular Python class.

If this is happening on a regular Python class then it sounds like a bug and you should report it (with full reproducible example) at github.com/cython/cython/issues

D Woods

unread,
May 27, 2021, 2:47:09 AM5/27/21
to cython-users
Yeah looks like a bug to me. I reported it https://github.com/cython/cython/issues/4196
Reply all
Reply to author
Forward
0 new messages