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

Add type annotation stubs #1103

Open
malmaud opened this issue Aug 9, 2018 · 21 comments
Open

Add type annotation stubs #1103

malmaud opened this issue Aug 9, 2018 · 21 comments
Assignees
Labels
feature something new P2 considered for next cycle

Comments

@malmaud
Copy link
Contributor

malmaud commented Aug 9, 2018

Hi,
I was wondering if you would be interested in PRs that add type annotation stubs for the plotly.py API. That would enable static type checkers like mypy and Pyre to statically type check Python projects using plotly, and as well as enhancing the IDE experience for editors able to offer richer experiences from type annotations (just as VSCode and PyCharM).

@jonmmease
Copy link
Contributor

Hi @malmaud, I would love to do this... but we still support Python 2.7.

That said, in all of our new generated code we use numpydoc docstrings with return types (see, for example, plotly/graph_objs/_bar.py). This is enough for PyCharm to figure things out (I haven't really tested other editors very much) and it does provide a nice experience when working with objects in the graph_objs hierarchy.

I would be interested in gaining a better understanding of the most broadly compatible way to specify type information for a mixed Python 2/3 project!

@malmaud
Copy link
Contributor Author

malmaud commented Aug 9, 2018 via email

@jonmmease
Copy link
Contributor

That looks really interesting. I don't see any downside to adding these to at least the code generation output, because that would automatically stay up to date (and this covers the majority of the public API).

What has your experience been like keeping these up to date as a codebase evolves (for the human generated code 🙂)? What kind of tooling/testing would you recommend to that help flag inconsistencies?

@malmaud
Copy link
Contributor Author

malmaud commented Aug 14, 2018

Well, I won't exaggerate - I became excited about static type checking for Python and started using it for my own research code, but I don't have have direct experience applying it to large multi-developer codebases. I'd look forward to trying and learning, however.

In terms of help from tooling, the only thing I know of is https://github.com/Instagram/MonkeyType. It attempts to produce stubs automatically based on the types it observes variables take during runtime, but I haven't tried it.

The good news is the worse-case scenario for an incorrect stub is a user gets an incorrect warning (or lack of warning) from their type checker and reports it as an issue. Actual runtime behavior can never be affected by incorrect stubs.

@jonmmease
Copy link
Contributor

If you're interested in digging in, I'd like to start by adding stub generation to the graph_obj code generation logic. The current logic is in codegen/datatypes.py. As I said, this is the majority of the public API, and we can keep it correct for free.

Once we have this, I'd like to do a bit of an editor survey to get a full list of the Python editors that support completion across the full nested graph_objs hierarchy because of the addition of the stubs (as I mentioned, PyCharm does fine with just the types in the numpydoc strings).

Then I think we'd have enough information to make an informed decision about how much benefit users would get from the slight increase in maintenance burden.

How does that sound?

@malmaud
Copy link
Contributor Author

malmaud commented Aug 15, 2018

Sounds great.

I should mention that there's a formalized generalization of PyCharm's docstring-aware approach whereby type annotations can be encoded into strings and put as comments inline with a source file, instead of as a separate stub (https://www.python.org/dev/peps/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code). I personally feel this is less elegant than separate stubs that get to use the nice new Python 3 syntax for type annotations and I'm probably less motivated personally to contribute annotations in that format, but it's a possibility.

@malmaud
Copy link
Contributor Author

malmaud commented Feb 12, 2019

I'm going to link to pytorch/pytorch#12500 as an example of a big project that went through the process of adding stubs and is discussing testing strategies etc - might be a useful reference for me in the future or whoever else wants to take a stab at this.

@ldorigo
Copy link

ldorigo commented Nov 24, 2020

Hey, I'm wondering if there is any news on this? With powerful static typecheckers such as Pylance (the new language server in VSCode) becoming widely available and adopted, it would be very nice to have decent typing information available for plotly objects.

@nicolaskruchten
Copy link
Contributor

There's no news at this time, but if someone wants to pick up the torch and work on a PR or proposal for how to get this done, we'd happily consider it and help you out :)

@jonmmease
Copy link
Contributor

Also, depending on how large they are (we have a lot or graph object classes), we may need to ship them in a separate package.

@harahu
Copy link

harahu commented Jun 29, 2021

Since python 2 has reached EOL now, I guess maintaining support for it should not be part of the consideration any more?

@JP-Ellis
Copy link
Contributor

As pointed out by @technic in (this comment)[https://github.com//issues/1682#issuecomment-922361704] from #1682, it looks like a simple workaround is to make use to the TYPE_CHECKING which is specifically designed to handle lazy imports.

@nicolaskruchten
Copy link
Contributor

I've added some very basic output type annotations to various functions that return go.Figure in this PR: #3708 but clearly there is lots of room to do more work in this area.

@gvwilson
Copy link
Contributor

Hi - we are currently trying to tidy up Plotly's public repositories to help us focus our efforts on things that will help users most. Since this issue has been sitting for several years, so I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our backlog. Thanks for your help - @gvwilson

@gustavi
Copy link

gustavi commented Jun 14, 2024

@gvwilson I understand your message but this issue was the second most liked in the repository. I think you can just re-open it because it still a valid request.

@gvwilson gvwilson reopened this Jun 14, 2024
@gvwilson gvwilson self-assigned this Jun 14, 2024
@gvwilson
Copy link
Contributor

Thanks @gustavi - I've reopened it and assigned it to myself, and I'll try to get it into our backlog.

@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added feature something new P3 backlog and removed enhancement labels Aug 12, 2024
@gvwilson
Copy link
Contributor

@T4rk1n is this related to the work you're currently doing on code generation?

@T4rk1n
Copy link

T4rk1n commented Aug 12, 2024

@T4rk1n is this related to the work you're currently doing on code generation?

No

@gvwilson gvwilson self-assigned this Feb 6, 2025
@gvwilson gvwilson added P2 considered for next cycle and removed P3 backlog labels Feb 6, 2025
@ClaasRostock
Copy link

I have created a first version of a type stub package for plotly.
GitHub: plotly-stubs
PyPI: plotly-stubs

@gvwilson
Copy link
Contributor

Thanks very much for this @ClaasRostock - would you have a few minutes to chat some time this week about adding types directly to plotly.py? If so, I'm [email protected] - thanks @gvwilson

@ClaasRostock
Copy link

Hi Greg @gvwilson ,
of course. This week is a bit tight, but next week is better. I will contact you by email, so we can find a common time.
Best regards,
Claas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

10 participants