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

feat: Adds vl_convert.pyi type stub #185

Merged
merged 8 commits into from
Aug 31, 2024
Merged

feat: Adds vl_convert.pyi type stub #185

merged 8 commits into from
Aug 31, 2024

Conversation

dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Aug 28, 2024

Closes #184

Updated

Tried enabling pyright on the tests/ directory and answered my own question on Option.
With that fixed, this is an example in VSCode:

image

Suggestion

You could setup mypy to run on tests/ to:

  • Confirm whatever you've expected in a test will not raise a warning for users
  • Notify if stubs become out of sync with the actual interface

Comment on lines 17 to 78
[tool.ruff]
target-version = "py310"
line-length = 88
indent-width = 4
exclude = []

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = true
line-ending = "lf"
# https://docs.astral.sh/ruff/formatter/#docstring-formatting
docstring-code-format = true
docstring-code-line-length = 88

[tool.ruff.lint]
# https://docs.astral.sh/ruff/preview/
preview = true

# https://docs.astral.sh/ruff/settings/#lint_extend-safe-fixes
extend-safe-fixes = [
# from __future__ import annotations #
# ---------------------------------- #
"UP006",
"UP007",
"UP008",
"TCH",
# unsorted-dunder-all
"RUF022",
# pydocstyle #
# ---------- #
# fits-on-one-line
"D200",
# escape-sequence-in-docstring
"D301",
# ends-in-period
"D400",
]
extend-select = [
"ANN",
"D",
"D213",
"D400",
"E",
"F",
"FA",
"I001",
"RUF",
"TCH",
"TID",
"UP",
"W",
]
ignore = [
# indent-with-spaces
"D206",
# multi-line-summary-first-line ((D213) is the opposite of this)
"D212",
# Line too long
"E501",
]
pydocstyle.convention = "numpy"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a subset of https://github.com/vega/altair/blob/main/pyproject.toml to try and keep a consistent style with altair.

AFAIK these only apply locally, since vl-convert is using black.

Comment on lines +104 to +119
VegaThemes: TypeAlias = Literal[
"carbong10",
"carbong100",
"carbong90",
"carbonwhite",
"dark",
"excel",
"fivethirtyeight",
"ggplot2",
"googlecharts",
"latimes",
"powerbi",
"quartz",
"urbaninstitute",
"vox",
]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lifted direct from the result of vega/altair#3523

if TYPE_CHECKING:
from typing import Any, Literal, TypeAlias

FormatLocaleName: TypeAlias = Literal[
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FormatLocaleName & TimeFormatLocaleName I got by following the links that you had in the docs.

The repos seem to have remained pretty constant, so maintaining these (hypothetically) would be on the scale of 2-7 years between changes.

Renderer: TypeAlias = Literal["canvas", "hybrid", "svg"]
FormatLocale: TypeAlias = FormatLocaleName | dict[str, Any]
TimeFormatLocale: TypeAlias = TimeFormatLocaleName | dict[str, Any]
VlSpec: TypeAlias = str | dict[str, Any]
Copy link
Member Author

@dangotbanned dangotbanned Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this alias (VlSpec) could be a placeholder for if you decided to get more specific in the future.

E.g. if you expect a certain level of nesting, this can be updated and propagate to all relevent defs.

The screenshots in vega/altair#3536 show examples of the utility of this

Comment on lines 441 to 455
Parameters
----------
vg_spec
Vega JSON specification string or dict
allowed_base_urls
List of allowed base URLs for external data requests.
Default allows any base URL
format_locale
d3-format locale name or dictionary
time_format_locale
d3-time-format locale name or dictionary

Returns
-------
scenegraph.
Copy link
Member Author

@dangotbanned dangotbanned Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to all docstrings, but I've omited the types since we have them in the signature.

Also most of the descriptions repeat the same information - but I've kept that in

renderer: Renderer | None,
) -> str:
"""
Convert a Vega-Lite spec to self-contained HTML document using a particular version of the Vega-Lite JavaScript library.
Copy link
Member Author

@dangotbanned dangotbanned Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to disable line-too-long for docs like this.

Splitting it into 2 lines broke multi-line-summary-second-line.

Just thought I'd note this with links to the rules, not proposing any changes

@jonmmease
Copy link
Collaborator

Hi @dangotbanned, thank for working on this. I checked out the branch to review and updated the type signatures for Python 3.8+ compatibility, since vl-convert is a bit more conservative than vega-altair (I try to use current vl-convert with prior versions of Vega-Altair).

Take a look and see if that makes sense to you, then I'll merge it

@dangotbanned
Copy link
Member Author

dangotbanned commented Aug 31, 2024

Hi @dangotbanned, thank for working on this. I checked out the branch to review and updated the type signatures for Python 3.8+ compatibility, since vl-convert is a bit more conservative than vega-altair (I try to use current vl-convert with prior versions of Vega-Altair).

Take a look and see if that makes sense to you, then I'll merge it

Thanks @jonmmease!

On the 3.8 compatibility, did the stub cause any runtime issues for you?
If not, I would suggest reverting those changes since AFAIK .pyi files are allowed to use features from newer versions https://typing.readthedocs.io/en/latest/spec/distributing.html#stub-files

Edit

Looking at the jsonschema stubs - I think we only need the from typing_extensions import TypeAlias

@dangotbanned
Copy link
Member Author

dangotbanned commented Aug 31, 2024

@jonmmease I'm thinking this is unrelated to that series of changes?

https://github.com/vega/vl-convert/actions/runs/10645388913/job/29511135603?pr=185

Maybe related but I absolutely botched the merge and had to overwrite the whole file with what I had in 4b8adbb (#185)

@jonmmease
Copy link
Collaborator

Yes, that CI failure is unrelated. There seems to be an inconsistency in the files that are pulled down from skypack, where they sometimes include a trailing comment. I've been meaning to update the vendoring logic to strip out trailing comments for this reason.

I'll restart CI and see if that resolves it for now.

On the 3.8 compatibility, did the stub cause any runtime issues for you?
If not, I would suggest reverting those changes since AFAIK .pyi files are allowed to use features from newer versions

Oh, you're right, these files are only for mypy 🤦‍♂️ Looks like you're merge reverted to the original form, so that works for me.

@jonmmease
Copy link
Collaborator

It passes now and looks good to me. Are you ready for it to merge?

@dangotbanned
Copy link
Member Author

It passes now and looks good to me. Are you ready for it to merge?

Perfect, yeah I'm happy for you to merge

@jonmmease jonmmease merged commit 8f5200c into main Aug 31, 2024
10 checks passed
@jonmmease jonmmease deleted the py-manual-stubs branch August 31, 2024 14:41
dangotbanned added a commit to vega/altair that referenced this pull request Oct 6, 2024
Will open a new PR to merge this, related to vega/vl-convert#185
dangotbanned added a commit to vega/altair that referenced this pull request Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

python: Generate/write (basic) type stubs
2 participants