-
-
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
feat: add type annotations to generated code #5008
Draft
gvwilson
wants to merge
6
commits into
main
Choose a base branch
from
type-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+124,381
−366,345
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
693cd5e
to
8da092c
Compare
1. Add `bin/get_size.py` so that `python bin/get_size.py plotly build` reports the number of files and total size in bytes of the `plotly` directory (where generated code is put) and the `build` directory that is populated by `python setup.py build`. 1. Modify `codegen/__init__.py` and `./setup.py` so that `python setup.py --reformat=false` disables reformatting. 1. Assign an empty string to the `data_docs` field of generated validators. (This has a major impact because those docs are duplicated many times.) 1. Alias name of base validator during import in `codegen/validators.py`. 1. Remove the long list of CSS colors from help strings for color properties. 1. Replace `super(Parent, self)` with `super()` in generated code. 1. Drop use of sys.version_info and TYPE_CHECKING. Removed the check for Python < 3.7 using `sys.version_info` and as a backup checking `typing.TYPE_CHECKING`; this saves a little space and also cleans up the code. 1. Remove mention of Chart Studio and explicit enumeration of system font names from plotly.js / plot-schema.json so that this text isn't copied dozens of times into the plotly.py bundle. 1. Introduce `_init_provided()` for `BaseFigure` and `BasePlotlyType` that calls a helper function `_initialize_provided()` to replace repetitions of: ``` _v = arg.pop("something", None) _v = something if something is not None else _v if _v is not None: self["something"] = _v ``` Original size of plotly/**/*.py: 42283582 bytes Current size of plotly/**/*.py: 31931739 bytes Change: -25%
1. Add `from __future__ import annotations` as the first line of generated files so that modern Python type annotation syntax can be used in older versions of Python. 1. Add `from typing import Any` and `from numpy.types import NDArray` to all generated files rather than trying to figure out which of these imports are needed on a file-by-file basis. 1. Rename `get_typing_type` in `codegen/datatypes.py` to `get_python_type` to make purpose clearer. 1. Add additional optional parameter to `get_python_type` so that `compound` and `compound_array` types in the schema are converted to `None` rather than causing an exception. 1. Modify `codegen/datatypes.py` to add type annotations to constructor parameters. 1. Modify `codegen/figure.py` to add `bool` type to one figure constructor parameter. FIXME: figure out what type should actually be returned for `compound` and `compound_array`. FIXME: figure out what types to use for other standard parameters of figures generated by `codegen/figure.py`.
8da092c
to
f949f65
Compare
1. Modify `commands.py` to run code generation. 1. Remove comments from generated code. 1. Replaced named arguments in constructors with positional arguments. 1. Regenerate all code. Notes: The generated code is reformatted once again: this slightly increases size but makes it more readable. There is one flaky test: `tests/test_plotly_utils/validators/test_colorscale_validator.py::test_acceptance_named[Inferno_r]` It fails when the entire test suite is run but does *not* fail when only `test_colorscale_validator.py` is run (using Python 3.11.9). | branch | format | bytes | %age | | -------- | ------- | -------- | ---- | | master | .whl | 14803768 | | | codegen2 | .whl | 12215667 | -18% | | master | .tar.gz | 8100014 | | | codegen2 | .tar.gz | 6114458 | -24% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
feat: adding type annotations to some constructor parameters
Using
get_typing_type
fromcodegen/datatypes.py
to add Python datatype to some constructor parameters.Notes:
Does not handle the
compound
datatype yet.get_typing_type
raises a
ValueError
because it doesn't have a clause for thistype;
add_constructor_params
catches this and simply doesn't adda type definition. Something between a third and half of all
constructor parameters suffer from this, so we need to figure out
what
compound
means and define a type for it. (SeeFIXME
inthe
codegen
directory.)Almost all parameters are nullable, so the type is defined as
(for example)
int
but the default value assigned isNone
.Depends on feat: modifying code generation to reduce bundle size #4978.