Skip to content
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

Define stubs for pytypes #3991

Closed
dbarnett opened this issue May 14, 2020 · 7 comments
Closed

Define stubs for pytypes #3991

dbarnett opened this issue May 14, 2020 · 7 comments
Labels
stubs: request OUTDATED! Request to add stubs for a new package to typeshed

Comments

@dbarnett
Copy link

Could typeshed include stubs for the pytypes library?

Currently if I enable runtime checking with decorators like @pytypes.typechecked I ironically lose static type information for the code and all the types come out to Any.

@JelleZijlstra
Copy link
Member

We definitely could! It might be even better though if the library itself bundled stubs or inline type information.

@dbarnett
Copy link
Author

dbarnett commented May 14, 2020

Isn't that blocked on PEP 561 though?

@JelleZijlstra
Copy link
Member

PEP 561 was accepted years ago.

@srittau srittau added the stubs: request OUTDATED! Request to add stubs for a new package to typeshed label May 28, 2020
@dbarnett
Copy link
Author

Ah, I found pytypes has Stewori/pytypes#74 tracking this. I can follow up there.

Thanks for helping me get my bearings!

@dbarnett
Copy link
Author

dbarnett commented Aug 5, 2020

Actually, I'm working on bundling the types inside pytypes, but having trouble telling if I'm doing it right. Do you know if there's any sort of developer guide beyond what's in PEP 561? Would you consider posting a pointer to something like that in typeshed's README to help get more libraries bundling their own types and not putting maintenance burden on typeshed?

A couple specific things I'm stuck on:

  • If I install a local copy of the library with types correctly defined, should mypy/pytype/pyls pick them up, or do I need to do anything special to make that work, like flush some cache? I'm installing everything into a virtualenv with python setup.py install, but no matter what I do it simply won't pick up the types I've added.
  • If I explicitly add stubs in a .pyi file but don't list every type that's in the .py file, will typecheckers still pick up some types from the .py, or will it give "no such attribute" errors for anything I don't explicitly include in the .pyi? Does .pyi "extend" or "replace" the definitions in .py?
  • If there are typechecker errors in the .py files and I add the py.typed marker, will that cause any ill effects?

@dbarnett dbarnett reopened this Aug 5, 2020
@dbarnett
Copy link
Author

dbarnett commented Aug 5, 2020

Aha, one big piece I was missing is that I'd been banging on all this stuff to fix pytype behavior, but pytype doesn't support PEP 561 at all yet, just typeshed (google/pytype#151). If I focus on mypy then a lot more starts to make sense.

In mypy the .pyi files do "replace" instead of "extending" definitions from .py, so any definitions not omitted from the .pyi will give you "Module has no attribute" errors from mypy.

It seems to fail gracefully if there are type checker issues in the library with bundled types, so IIUC you don't need to have all your code perfect with no typechecker complaints before adding py.typed, just if any type definitions are invalid mypy complains in code that tries to use those bad definitions.

Still, if there's a guide you could create or point to, there's plenty of useful context there that would have been great to learn about upfront.

@dbarnett dbarnett closed this as completed Aug 5, 2020
@srittau
Copy link
Collaborator

srittau commented Aug 5, 2020

Unfortunately, the typing documentation is a bit spread out between various PEPs, Python's typing documentation, and mypy's documentation, with a few sprinkles in typeshed's CONTRIBUTING file. I don't know about any comprehensive guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: request OUTDATED! Request to add stubs for a new package to typeshed
Projects
None yet
Development

No branches or pull requests

3 participants