question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Py3 mode renames "str" type to "unicode" in auto-embedded signature

See original GitHub issue

Consider this:

$ cat mylib.pyx
#cython: embedsignature=True
def listdir(str path):
    '''Print out *path*'''

    print(path)

$ cython --version
Cython version 0.19

$ cython mylib.pyx; grep 'Print out' mylib.c | head -1
static char __pyx_doc_5mylib_listdir[= "listdir(str path)\nPrint out *path*";

$ cython -3 mylib.pyx; grep 'Print out' mylib.c | head -1
static char __pyx_doc_5mylib_listdir[](]) = "listdir(unicode path)\nPrint out *path*";

In other words, when running with -3, the str type in the function signature becomes unicode.

I guess the idea is that when compiled for Python 2, the extension will expect a unicode object. However, when compiled under Python 3 (which is probably the more likely case), the signature is just confusing as there is no unicode type.

Moreover, the guess that under Python 2.x the function expects an unicode object is not necessarily correct. In my case, the function truly expects str in both Python 2.x and 3.x. Therefore, I think it would be just not rename any types when generating the docstring signature.

Migrated from http://trac.cython.org/ticket/812

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
dgrunwaldcommented, Sep 20, 2019

This bug also affects the inspect.signature() result:

# cython: binding=True
def f(path: str, a: int = 0, b: bool = True) -> typing.List[str]:
    pass
>>> testmodule.f.__annotations__
{'path': 'unicode', 'a': 'int', 'b': 'bool', 'return': typing.List[str]}
0reactions
fahhemcommented, Apr 12, 2022

This seems to cause errors with py3 typing code that uses str as a type.

Original source code (some YAML handling code):

AnyValue = Union[float, str, int, Dict]

Error at runtime:

Traceback (most recent call last):
...snip...
    AnyValue = Union[float, str, int, Dict]
NameError: name 'unicode' is not defined

The code runs under CPython3, but fails due to this rewriting. I’d be happy to send a PR up, though I can’t find where the str->unicode translation is happening. I looked at AutoDocTransforms though I don’t see how that does the translation, nor how I would use it to avoid the translation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unicode HOWTO — Python 3.11.1 documentation
Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters.
Read more >
Unicode & Character Encodings in Python: A Painless Guide
Encoding and Decoding in Python 3. Python 3's str type is meant to represent human-readable text and can contain any Unicode character.
Read more >
NameError: global name 'unicode' is not defined - in Python 3
Python 3 renamed the unicode type to str , the old str type has been replaced by bytes . if isinstance(unicode_or_str, str): text...
Read more >
Porting to Python 3 | Django documentation
Python 2's unicode() type was renamed str() in Python 3, str() was renamed bytes(), and basestring() disappeared. six provides tools to deal with...
Read more >
Byte string, Unicode string, Raw string — A Guide to all strings ...
However, do you know there are at least four types of… ... Byte string and Unicode String (default Python3 string) — It's all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found