Skip to content

Commit

Permalink
Merge pull request #3142 from plotly/fix/dates-types-id
Browse files Browse the repository at this point in the history
Fix dates and id typing
  • Loading branch information
T4rk1n authored Feb 5, 2025
2 parents 7caf01e + 3b7dba8 commit e77aafb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [3.0.0-rc2] - UNRELEASED

## Fixed

- [#3142](https://github.com/plotly/dash/pull/3142) Fix typing generation for id and dates props.

## [3.0.0-rc1] - 2025-01-28

## Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'react-virtualized-select/styles.css';
import '../components/css/[email protected]';
import '../components/css/Dropdown.css';

import {propTypes, defaultProps} from '../components/Dropdown.react';
import {propTypes} from '../components/Dropdown.react';
import {sanitizeOptions} from '../utils/optionTypes';
import isEqual from 'react-fast-compare';

Expand Down Expand Up @@ -168,6 +168,5 @@ const Dropdown = props => {
};

Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;

export default Dropdown;
37 changes: 36 additions & 1 deletion dash/development/_py_prop_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def generate_enum(type_info, *_):
def get_prop_typing(
type_name: str, component_name: str, prop_name: str, type_info, namespace=None
):
if prop_name == "id":
# Id is always the same either a string or a dict for pattern matching.
return "typing.Union[str, dict]"

if namespace:
# Only check the namespace once
special = (
Expand All @@ -141,7 +145,38 @@ def generate_plotly_figure(*_):
return "typing.Union[Figure, dict]"


special_cases = {"dash_core_components": {"Graph": {"figure": generate_plotly_figure}}}
def generate_datetime_prop(component, array=False):
if "import datetime" not in custom_imports["dash_core_components"][component]:
custom_imports["dash_core_components"][component].append("import datetime")

def generator(*_):
datetime_type = "typing.Union[str, datetime.datetime]"
if array:
datetime_type = f"typing.Sequence[{datetime_type}]"
return datetime_type

return generator


special_cases = {
"dash_core_components": {
"Graph": {"figure": generate_plotly_figure},
"DatePickerRange": {
"start_date": generate_datetime_prop("DatePickerRange"),
"end_date": generate_datetime_prop("DatePickerRange"),
"min_date_allowed": generate_datetime_prop("DatePickerRange"),
"max_date_allowed": generate_datetime_prop("DatePickerRange"),
"disabled_days": generate_datetime_prop("DatePickerRange", True),
},
"DatePickerSingle": {
"date": generate_datetime_prop("DatePickerSingle"),
"min_date_allowed": generate_datetime_prop("DatePickerSingle"),
"max_date_allowed": generate_datetime_prop("DatePickerSingle"),
"disabled_days": generate_datetime_prop("DatePickerSingle", True),
"initial_visible_month": generate_datetime_prop("DatePickerSingle"),
},
}
}


PROP_TYPING = {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/development/metadata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def __init__(
optionalAny: typing.Optional[typing.Any] = None,
customProp: typing.Optional[typing.Any] = None,
customArrayProp: typing.Optional[typing.Sequence[typing.Any]] = None,
id: typing.Optional[str] = None,
id: typing.Optional[typing.Union[str, dict]] = None,
**kwargs
):
self._prop_names = ['children', 'id', 'aria-*', 'customArrayProp', 'customProp', 'data-*', 'in', 'optionalAny', 'optionalArray', 'optionalArrayOf', 'optionalBool', 'optionalElement', 'optionalEnum', 'optionalNode', 'optionalNumber', 'optionalObject', 'optionalObjectOf', 'optionalObjectWithExactAndNestedDescription', 'optionalObjectWithShapeAndNestedDescription', 'optionalString', 'optionalUnion']
Expand Down

0 comments on commit e77aafb

Please sign in to comment.