-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add stubs for cython #1554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stubs for cython #1554
Conversation
This enables us to do type annotations for Cython's built-in C types. For example, def foo(s: cython.str) -> cython.int: ...
Thanks for creating these stubs! Can you ask on the Cython issue tracker for permission to add stubs to typeshed? That's the standard procedure for third-party libraries. Also, why aren't the stubs in 2and3? |
Yuan-Chao, we could just put all your stubs in 2and3, if they don't work under Python 2 and somebody finds out, they'll fix it. |
Got it. I'm doing that. |
Permission granted in cython/cython#1826. |
third_party/3/Cython/Shadow.pyi
Outdated
void = Union[None] | ||
basestring = py_str | ||
|
||
gs: Dict[str, Any] # Should match the return type of globals() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gs
should not be exported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? We are in fact using it in the .pyi generator. Example:
# Some legal Cython C types are not defined in cython, so may need to be added
# here.
ctypes = {repr(v): k for k, v in cython.gs.items()
if isinstance(v, cython.typedef)}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, legacy... You forget to strip something from a namespace and people will just start picking it up and using it in their code. I didn't write that code but I have hight doubts that it was intended to keep it in there. It does not have a counterpart in Cython's own namespace. Let's see if @robertwb has an opinion on this, but I guess we'll have to leave it in there then...
third_party/2and3/Cython/Shadow.pyi
Outdated
def __init__(self, type: _T, name: Optional[str] = ...) -> None: ... | ||
def __call__(self, *arg) -> _T: ... | ||
def __repr__(self) -> str: ... | ||
__getitem__ = index_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a lot more in Shadow.py that isn't in the stub, e.g. @cython.inline
. Should we add those to prevent false positive type errors for people using those functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JelleZijlstra Currently we only have what we are using at hand. Can we let the remaining be added gradually when they are used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with that is that people who use these things will now get spurious mypy errors. Therefore, I'd prefer to include all names that are in the stub, even if perhaps their types are incomplete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add those to prevent false positive type errors for people using those functions?
Sure. Help welcome, as always.
third_party/2and3/Cython/Shadow.pyi
Outdated
name: str | ||
|
||
def __init__(self, type: _T, name: Optional[str] = ...) -> None: ... | ||
def __call__(self, *arg) -> _T: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add : Any
.
We could use |
See cython/cython#1965 for some minor additions. Would it be possible to host this in Cython, and ship a copy in typeshed for convenience for cases where Cython is not installed? |
Yes, mypy now supports using stubs from installed library modules: see https://mypy.readthedocs.io/en/latest/installed_packages.html#making-pep-561-compatible-packages. If Cython chooses to ship its own stubs, I don't think it makes much sense to also put stubs in typeshed, since they'd only be useful in the rare case where somebody wants to type check stubs that use Cython, but does not actually have Cython installed. |
I am going to close this here, since Cython seems to ship its own |
This enables us to do type annotations for Cython's built-in C types.
For example,
def foo(s: cython.str) -> cython.int: ...