You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The main function optimise_fonts and the other exposed functions using it return dict[str, typing.Any]. This is inconvenient for the users of the library as they will have no autocomplete and type safety. There are two possible ways you can solve it. I would recommend 1 here, although it will break backwards compatibility. You can use 2 if you really prefer dictionaries and don't want to break the backwards compatibility.
1. Using a dataclass:
Returning a suitable dataclass instead will allow users to access the values with dot notation:
return dict[str, typing.Any]. This is inconvenient for the users of the library as they will have no autocomplete and type safety
Yes! This is a great point, and worried me when I wrote it. I had no idea how to represent multiple types in a type hint; without your feedback here I likely would have gone in the direction of writing a class. That's also because there are multiple places in code where an empty/default return value is laboriously constructed, which is ugly.
But both solutions you have here are new to me: both typed dict and data classes. I'm not attached to a dict per se, it just seemed a neat / Pythonic way to collect multiple items together. Thanks for the feedback and I'll research both, but I note the recommendation for (1) and will pay attention there.
Hi. I have been thinking about this for a while, and I'm still trying to figure out a solution that lets me retain the existing interface while adding another, in order not to break the library usage.
The main function
optimise_fonts
and the other exposed functions using it returndict[str, typing.Any]
. This is inconvenient for the users of the library as they will have no autocomplete and type safety. There are two possible ways you can solve it. I would recommend 1 here, although it will break backwards compatibility. You can use 2 if you really prefer dictionaries and don't want to break the backwards compatibility.1. Using a dataclass:
Returning a suitable dataclass instead will allow users to access the values with dot notation:
Then you can return an instance of this instead of your dictionary:
2. Using TypedDict:
Return a typed dictionary. The code would be something along these lines:
The text was updated successfully, but these errors were encountered: