-
Notifications
You must be signed in to change notification settings - Fork 795
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
fix(typing): Resolve mypy==1.11.0
issues in plugin_registry
#3487
Conversation
R = TypeVar("R") | ||
Plugin = TypeAliasType("Plugin", Callable[..., R], type_params=(R,)) | ||
PluginT = TypeVar("PluginT", bound=Plugin[Any]) | ||
IsPlugin = Callable[[object], TypeIs[Plugin[Any]]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an example of how this could be used.
@runtime_checkable
would not be useful as it doesn't check types. Any callable would pass, regardless of the return type.
alt.utils.theme.py
from .plugin_registry import PluginRegistry
from typing import Callable
from typing_extensions import TypeAlias, TypeIs
ThemeType: TypeAlias = Callable[..., dict]
def is_theme_plugin(obj: Callable[..., Any]) -> TypeIs[ThemeType]:
from inspect import signature
from typing import get_origin
sig = signature(obj)
ret = sig.return_annotation
return ret is dict or get_origin(ret) is dict
class ThemeRegistry(PluginRegistry[ThemeType, dict]):
pass
alt.vegalite.v5.theme.py
from typing import Final
from ...utils.theme import ThemeRegistry, is_theme_plugin
ENTRY_POINT_GROUP: Final = "altair.vegalite.v5.theme"
themes = ThemeRegistry(entry_point_group=ENTRY_POINT_GROUP, plugin_type=is_theme_plugin)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the fix!
Fixes
Description
The release of Mypy 1.11 introduced stricter typing for
functools.partial
.The existing
# type: ignore
comments were no longer sufficient and correcting everything (whilst not breaking all the derived registries) was more complex than I'd like.Hopefully this summary is easier to understand than the diff alone.
Fix
The fix is mostly 2 changes:
assert isinstance(...)
to a narrowing functionFor 2, I have added a courtesy deprecation.
No current
altair
code (including tests) used theplugin_type
argument.I don't think
PluginRegistry
is intended to be public as it doesn't appear inalt.__all__
.The new default has identical behaviour to the previous default
It is also safer as it won't be disabled by optimizations & better adheres to the current typing spec guidance.