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

Allow arbitrary attributes in HTMLDependency's stylesheet and script objects #83

Open
gadenbuie opened this issue Apr 5, 2024 · 2 comments

Comments

@gadenbuie
Copy link
Contributor

I appreciate the effort in this type (and the one for script objects as well), but I think it might be overly limiting.

StylesheetItemExtraAttrs = TypedDict(
"StylesheetItemExtraAttrs",
{
"as": str,
"crossorigin": str,
"disabled": str,
"hreflang": str,
"imagesizes": str,
"imagesrcset": str,
"integrity": str,
"media": str,
"prefetch": str,
"referrerpolicy": str,
"rel": str,
"sizes": str,
"title": str,
"type": str,
},
total=False,
)

I recently wanted to add a data- attribute to the <link> tag for a set of stylesheets in the shinyswatch HTMLDependency. I was pleased that stylesheet implementation lets me pass arbitrary attributes, but I had to disable type checking for the input.

I also noticed that the presence of these types did not lead to any hints in the IDE, just type errors when checked. That might be a configuration issue on my end, or it might signal that the type annotation isn't as useful as expected.

Personally, I think the TagAttrValues type would balance flexibility and type checking.

@wch
Copy link
Collaborator

wch commented Apr 5, 2024

I think that we can allow other attributes with the new __extra_items__ syntax as described in https://peps.python.org/pep-0728/.

One of the useful things about the typed dictionaries is that they will tell you if you're missing a required field. The non-required fields can be useful for autocompletion.

image

(An aside: I can't remember why they're built the way they are, by combining two TypedDicts, one with required fields and one with non-required fields, instead of a single TypedDict with NotRequired for some of the fields. Perhaps this code was written before NotRequired was available?)

@gadenbuie
Copy link
Contributor Author

Oh I see, the part I was missing is that you have to open the dictionary key "" to trigger the autocomplete.

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

No branches or pull requests

2 participants