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

Support Python typing #395

Open
asokolsky opened this issue Sep 6, 2022 · 11 comments
Open

Support Python typing #395

asokolsky opened this issue Sep 6, 2022 · 11 comments
Labels
feature hacktoberfest Issues for participation in the hacktoberfest https://hacktoberfest.com/

Comments

@asokolsky
Copy link

I get:

Skipping analyzing "icalendar": module is installed, but missing library stubs or py.typed marker - mypy
@niccokunzmann
Copy link
Member

@asokolsky Thanks for reporting this:

  • how do you generate this text - it looks like from a tool
  • what would you like to see instead?
  • what would be ways to approach this?

@asokolsky
Copy link
Author

how do you generate this text - it looks like from a tool

I run mypy on my code which is using icalendar

what would you like to see instead?

No mypy warnings

what would be ways to approach this?

From https://mypy.readthedocs.io/en/stable/existing_code.html

Start small – get a clean mypy build for some files, with few annotations

  • Write a mypy runner script to ensure consistent results
  • Run mypy in Continuous Integration to prevent type errors
  • Gradually annotate commonly imported modules
  • Write annotations as you modify existing code and write new code
  • Use MonkeyType or PyAnnotate to automatically annotate legacy code

Hope this helps.

@angatha
Copy link
Collaborator

angatha commented Sep 30, 2022

Is this feasable for 4.x branches where the compatibility reaches back to 2.7 and 3.4? I remember that comments where used at that time to di type hinting which was changed in 3.5.

@asokolsky
Copy link
Author

Is this feasable for 4.x branches where the compatibility reaches back to 2.7 and 3.4?

Don't take my word for it, but I think it is:

@niccokunzmann
Copy link
Member

I am happy to see a pull request that shows some examples of how to go about it.

Since the code is tested, is there a way to harvest the hints from the tests?

An idea is to have a mypy branch that is based on master and that tests the annotations and can be worked on until it is green.

What do you think?

@djbrown
Copy link

djbrown commented Oct 1, 2022

AFAIK there are the following options:

  • inline type hints, not backwards compatible
  • seperate type stubs, created manually or via mypy stubgen
    the stubs may be worked on and published via one of the following primary ways:

@niccokunzmann
Copy link
Member

I would (maybe falsely) assume that people using type hints also like to use later Python versions that have them built in. The master branch would receive the changes, according to the README:

We expect the master branch with versions 5+ receive the latest updates and features,
and the 4.x branch the subset of security and bug fixes only.

As such, inline typing in the files icalendar files can be used - if I am right that Python 3.7+ supports it.

@niccokunzmann
Copy link
Member

niccokunzmann commented Oct 11, 2022 via email

@djbrown
Copy link

djbrown commented Oct 12, 2022

do you mean how to get started?

first of all you should install mypy: pip install mypy
then you check for typing issues: mypy src/icalendar/
and you can generate stub files, from which you can copy the type hints inline to the source code: stubgen src/icalendar/

@djbrown
Copy link

djbrown commented Oct 12, 2022

also relevant for this issue is PEP 561 – Distributing and Packaging Type Information

Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing.

I'm not familiar with distributing packages though, but if I understand correctly, you would have to create an empty file /src/icalendar/py.typed and then extend yout setup.py with:

setup(
    ...
    package_data = {
        'icalendar': ['py.typed'],
    },
    ...
)

@niccokunzmann
Copy link
Member

@djbrown @asokolsky @angatha thanks for the discussion! It is very clear to me now how to approach this and I am happy the see a pull request!

  • generate stubs and copy them into the code
  • run tests to make sure everything is typed (correctly)
  • add py.typed marker
  • make sure the marker is distributed

@niccokunzmann niccokunzmann added feature hacktoberfest Issues for participation in the hacktoberfest https://hacktoberfest.com/ and removed type: discussion labels Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature hacktoberfest Issues for participation in the hacktoberfest https://hacktoberfest.com/
Projects
None yet
Development

No branches or pull requests

5 participants