Skip to content

Commit

Permalink
respond to review comments test-py38 test-py39 test-py310 test-py311
Browse files Browse the repository at this point in the history
  • Loading branch information
benpankow committed Oct 1, 2023
1 parent a627a90 commit 091f4ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
21 changes: 16 additions & 5 deletions python_modules/dagster/dagster/_config/pythonic_config/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
NamedTuple,
Optional,
Set,
Tuple,
Type,
TypeVar,
Union,
Expand Down Expand Up @@ -865,16 +864,28 @@ def _is_annotated_as_resource_type(annotation: Type, metadata: List[str]) -> boo
)


class ResourceDataWithAnnotation(NamedTuple):
key: str
value: Any
annotation: Type
annotation_metadata: List[str]


def separate_resource_params(cls: Type[BaseModel], data: Dict[str, Any]) -> SeparatedResourceParams:
"""Separates out the key/value inputs of fields in a structured config Resource class which
are marked as resources (ie, using ResourceDependency) from those which are not.
"""
keys_by_alias = {field.alias: field for field in cls.__fields__.values()}
data_with_annotation: List[Tuple[str, Any, Type, List[str]]] = [
data_with_annotation: List[ResourceDataWithAnnotation] = [
# No longer exists in Pydantic 2.x, will need to be updated when we upgrade
(k, v, keys_by_alias[k].outer_type_, [])
for k, v in data.items()
if k in keys_by_alias
ResourceDataWithAnnotation(
key=field_key,
value=field_value,
annotation=keys_by_alias[field_key].outer_type_,
annotation_metadata=[],
)
for field_key, field_value in data.items()
if field_key in keys_by_alias
]
# We need to grab metadata from the annotation in order to tell if
# this key was annotated with a typing.Annotated annotation (which we use for resource/resource deps),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,25 @@ def safe_is_subclass(cls: Any, possible_parent_cls: Type) -> bool:


def is_optional(annotation: Type) -> bool:
"""Returns true if the annotation is Optional[T] or Union[T, None]."""
return (
get_origin(annotation) in (Union, UnionType)
and len(get_args(annotation)) == 2
and type(None) in get_args(annotation)
)
"""Returns true if the annotation signifies an Optional type.
In particular, this can be:
- Optional[T]
- Union[T, None]
- Union[None, T]
- T | None (in Python 3.10+)
- None | T (in Python 3.10+).
"""
# Optional[T] is equivalent to Union[T, None], see
# https://docs.python.org/3/library/typing.html#typing.Optional
# A user could also specify a Union themselves
if get_origin(annotation) == Union:
return len(get_args(annotation)) == 2 and type(None) in get_args(annotation)

# The Python 3.10 pipe syntax evaluates to a UnionType
# rather than a Union, so we need to check for that as well
if get_origin(annotation) == UnionType:
return len(get_args(annotation)) == 2 and type(None) in get_args(annotation)

return False

0 comments on commit 091f4ab

Please sign in to comment.