Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add type annotations to generated code #5008

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

Draft
gvwilson wants to merge 11 commits into main
base: main
Choose a base branch
Loading
from type-annotations
Draft

Conversation

Copy link
Contributor

@gvwilson gvwilson commented Feb 3, 2025
edited
Loading

feat: adding type annotations to some constructor parameters

Using get_typing_type from codegen/datatypes.py to add Python datatype to some constructor parameters.

Notes:

  1. Does not handle the compound datatype yet. get_typing_type
    raises a ValueError because it doesn't have a clause for this
    type; add_constructor_params catches this and simply doesn't add
    a 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. (See FIXME in
    the codegen directory.)

  2. Almost all parameters are nullable, so the type is defined as
    (for example) int but the default value assigned is None.

  3. Depends on feat: modifying code generation to reduce bundle size #4978 .

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`.
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% |
gvwilson added 3 commits March 19, 2025 09:28
1. Update required version of `black` in `pyproject.toml` and `.circleci/config.yml`.
2. Update Python version identifiers in `tool.black` section of `pyproject.toml` to include `py311` and `py312`.
3. Modify invocation of `black` in `commands.py` to format with `py311`.
4. Regenerate and reformat code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Labels
feature something new P1 needed for current cycle
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /