MCP Server Part 1: framework utilities and types#3710
MCP Server Part 1: framework utilities and types#3710
Conversation
|
Thank you for your contribution to Dash! 🎉 This PR is exempt from requiring a linked issue due to its labels. |
| import typing # noqa: F401 | ||
| from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401 | ||
| from dash.development.base_component import Component, _explicitize_args | ||
| from dash.types import NumberType # noqa: F401 |
There was a problem hiding this comment.
This import doesn't exist on previous versions of dash, to maintain backward compatibility, types are created on the generated file.
There was a problem hiding this comment.
I think I understand the backwards-compatiblity issue here, but tell me if I'm missing something:
- 3rd party components generated with this change would become incompatible with
dash<=4.1.0. - Our own dcc/html components are tied to the framework version, so there is no compatibility issue internally.
What do you think about "transitioning" the NumberType into the framework by including a conditional import in the generated components:
try:
from dash.types import NumberType
except ImportError:
NumberType = typing.Union[typing.SupportsFloat, typing.SupportsInt, typing.SupportsComplex]
This would maintain backwards compatibility while also giving us a type that we can change in the framework going forward.
There was a problem hiding this comment.
OK great - I pushed up that change.
| """Reusable layout utilities for traversing and inspecting Dash component trees.""" | ||
|
|
There was a problem hiding this comment.
Not sure about naming this file layout as this might confuse users with the import. Also common pattern is the user have a layout file/variable/function and importing this would need to be aliased in those case.
If this is not intended to be used by the dash app developer, then maybe rename to _layout, otherwise maybe something like layout_tools or layout_utils would be more explicit.
There was a problem hiding this comment.
Good call - I renamed this to _layout_utils.py.
| if TYPE_CHECKING: | ||
| from dash.types import NumberType # noqa: F401 | ||
| else: | ||
| try: | ||
| from dash.types import NumberType # noqa: F401 | ||
| except ImportError: | ||
| NumberType = typing.Union[ # noqa: F401 | ||
| typing.SupportsFloat, typing.SupportsInt, typing.SupportsComplex | ||
| ] |
There was a problem hiding this comment.
But now there's two import, since both definitions are types I think the else is redundant.
There was a problem hiding this comment.
Unfortunately there is a quirk here for pyright: the type checker is not satisfied with the try/except style of import and the Typing Tests fail.
I re-wrote this more compactly, but as far as I can tell we need to use the TYPE_CHECKING constant in order to satisfy pyright.
f4108e5 to
3f9fbca
Compare
Summary
For schema generation:
Componentclass compatible with PydanticNumberTypedefinition out from individual components into a generic place where the type can be introspected (and also made compatible with Pydantic)CallbackDispatchBody,CallbackDispatchResponse)Utilities
dash/layout.py)pydantic>=2.10to dependenciesManual testing
There isn't any visible feature, but you can try out the layout traversal and pydantic type exposure by hand: