Repository navigation
Add stubs for cython - #1554
Add stubs for cython#1554OswinC wants to merge 5 commits into
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. |
| void = Union[None] | ||
| basestring = py_str | ||
|
|
||
| gs: Dict[str, Any] # Should match the return type of globals() |
There was a problem hiding this comment.
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.
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...
| 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.
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.
@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.
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.
Should we add those to prevent false positive type errors for people using those functions?
Sure. Help welcome, as always.
| name: str | ||
|
|
||
| def __init__(self, type: _T, name: Optional[str] = ...) -> None: ... | ||
| def __call__(self, *arg) -> _T: ... |
|
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: ...