-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Improve Type Checking Support #3425
Conversation
As this changes part of the |
I've rebased the pull request on the master branch as this has included fixes for the previously failing tests. |
If there's any interest in this pull request, I'm happy to rebase so that it can merge with the most recent changes. |
I'm just a Plotly user/contributor (not a maintainer) but I'm very interested in having this merged. |
Still interested :) |
Could this get merged? Type hints are a huge help and this PR goes a long way to making them work... :-) |
Sorry for the long radio-silence here folks. This PR seems fine in principle although I'm a bit worried about it breaking something I'm not aware of. Does anyone know of any major dangers to this approach? If it could be rebased to the latest commit, I will kick the tires on it before the next release to see if I can break it. |
Thanks @nicolaskruchten. The only way I can see it causing an issue would be if |
Commit has been rebased, and code has been regenerated. For some reason, the regnerated code has the following change, though I'm not sure whether that is expected or not. Sets the tick label formatting rule using d3
formatting mini-languages which are very
similar to those in Python. For numbers, see: h
- ttps://github.com/d3/d3-format/tree/v1.4.5#d3-f
- ormat. And for dates see:
+ ttps://github.com/d3/d3-format/tree/v1.4.5#d3-
+ format. And for dates see:
https://github.com/d3/d3-time-
format/tree/v2.2.3#locale_format. We add two
items to d3's date formatter: "%h" for half of |
CI should be fixed now, if you can merge master into your branch please :) (with thanks to @maresb) |
This checks whether type checking is being done, and if so, disables the lazy import as this is generally not supported by type checkers. This should not have any impact on the runtime performance as the lazy import should still be used at runtime. This is achieved by inspecting the [`TYPE_CHECKING`](https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING) variable. This should resolve issue #1682 and provide a temporary workaround for issue #1103. In the latter case, the addition of stubs would probably still be an improvement as I believe this would be faster. Signed-off-by: JP-Ellis <josh@jpellis.me>
CI is now passing 🎉 |
Thanks! I'll kick the tires on this this week. Just so I'm clear on what the expected impact here is, could you describe what you see with/without this PR? Which tools should display a difference: VSCode, Jupyter Notebook, JupyterLab? I'm not a PyCharm user so I can't test there... It would be great to have before/after screenshots for various platforms if anyone can submit one :) |
OK the above message was a bit lazy: I'm downloading PyCharm now :) |
OK, I'm satisfied that this is safe :) I'd still like some clear examples about specific important interactions that folks are doing in certain IDEs which are improved by this PR. |
@nicolaskruchten, I use VS Code with Pylance for type checking. I was using the following ugly from typing import TYPE_CHECKING
from plotly.graph_objs import FigureWidget
if TYPE_CHECKING:
# Plotly does some conditional import stuff for the case that ipywidgets is missing.
# This confuses Pylance, so we help it by directly importing the real FigureWidget.
try:
from plotly.graph_objs._figurewidget import FigureWidget # noqa: F811, F401
except ImportError:
pass and then from hack import FigureWidget This achieves the desired type inference: otherwise, due to the dynamic import stuff, I end up with I haven't tested it myself, but I expect this PR to supersede my ugly hack, so I'm happy about that. Thanks so much!!! |
Since I can do it easily for VS Code, I just cloned master and installed it, and I can verify that my hack is no longer needed. Thanks again everyone! 😄 |
@nicolaskruchten Type checking allows the editor to have some insight into the code and assist the user. There are many ways this can help:
Note that while this PR allows the editor to do some introspection, as there are no type annotations in Plotly's code, the editor will still be unaware of the types of most things. In particular:
I am sure that the type annotations can be generated since most of the documentation includes type annotations, but this PR for now just allows the type checker / editor to see the code (as it was hidden behind lazy imports before). |
Thanks @JP-Ellis! I'm pretty familiar with what type checking does, but I want to be able to explain specifically what IDE interactions didn't work before but do now... So for example it seems like typing I'm working on getting some minimal type annotations for the chaining methods on |
Sorry, I wasn't sure what you were asking for, so my answer was quite broad. It should be the case now that
The type annotations would be a massive improvement to the usability of Plotly in the future 👍 |
Actually, sorry |
Can you validate this in your local editor? Does it work in VSCode? PyCharm? |
I can confirm that it all works as expect.
I am using VSCode with Pylance as the language server. Note that the order in which suggestions are offered varies based on usage. |
Ok thanks! Maybe I don't have Pylance or it's disabled for some reason... |
Hmm no, Pylance is installed and enabled etc. Here's how I'm testing it, maybe you're doing something different... I'm opening a new file, selecting Python as the language and then typing |
Yeah, not sure why this is happening. Doing the same thing for me gives At this stage, I don't think it is an issue with the PR though, I think this is a difference due to configurations. Can you check that |
Oh the PR is great, I'm just making sure I know how to explain it correctly in the upcoming announcement post on the forum :) Indeed, I found the settings in my VSCode which were preventing this from working perfectly, sorry for the noise :) |
And this works in JupyterLab without the language-server extensions too. Nice work @JP-Ellis ! |
Final note on this PR: as a result of finally merging it, I was able to do the work in this PR #3708 and get some nice easy wins! Thanks again :) |
Code PR Checklist
Code PR
plotly.graph_objects
, my modifications concern thecodegen
files and not generated files.modified existing tests.
new tutorial notebook (please see the doc checklist as well).
Prevent lazy import when type checking
This checks whether type checking is being done, and if so, disables the lazy import as this is generally not supported by type checkers. This should not have any impact on the runtime performance as the lazy import should still be used at runtime.
This is achieved by inspecting the
TYPE_CHECKING
variable.This should resolve issue #1682 and provide a temporary workaround for issue #1103. In the latter case, the addition of stubs would probably still be an improvement as I believe this would be faster.