Skip to content

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

Closed
wants to merge 5 commits into from
Closed

Add stubs for cython #1554

wants to merge 5 commits into from

Conversation

OswinC
Copy link

@OswinC OswinC commented Aug 17, 2017

This enables us to do type annotations for Cython's built-in C types.
For example,
def foo(s: cython.str) -> cython.int: ...

OswinC added 2 commits August 16, 2017 23:32
This enables us to do type annotations for Cython's built-in C types.
For example,
    def foo(s: cython.str) -> cython.int: ...
@JelleZijlstra
Copy link
Member

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?

@ambv
Copy link
Contributor

ambv commented Aug 21, 2017

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.

@OswinC
Copy link
Author

OswinC commented Aug 21, 2017

Got it. I'm doing that.

@scoder
Copy link

scoder commented Aug 22, 2017

Can you ask on the Cython issue tracker for permission to add stubs to typeshed?

Permission granted in cython/cython#1826.

void = Union[None]
basestring = py_str

gs: Dict[str, Any] # Should match the return type of globals()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gsshould not be exported.

Copy link
Contributor

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)}

Copy link

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...

scoder added a commit to cython/cython that referenced this pull request Aug 22, 2017
def __init__(self, type: _T, name: Optional[str] = ...) -> None: ...
def __call__(self, *arg) -> _T: ...
def __repr__(self) -> str: ...
__getitem__ = index_type
Copy link
Member

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?

Copy link
Author

@OswinC OswinC Sep 2, 2017

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?

Copy link
Member

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.

Copy link

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.

name: str

def __init__(self, type: _T, name: Optional[str] = ...) -> None: ...
def __call__(self, *arg) -> _T: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add : Any.

@JelleZijlstra
Copy link
Member

We could use def __getattr__(name: str) -> Any: ... to signify that the stub is incomplete for now and get this PR in.

@scoder
Copy link

scoder commented May 20, 2018

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?

@JelleZijlstra
Copy link
Member

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.

@srittau
Copy link
Collaborator

srittau commented Oct 21, 2018

I am going to close this here, since Cython seems to ship its own .pyi files now (even if they are not PEP 561-compatible, yet.)

@srittau srittau closed this Oct 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants