Skip to content

Commit

Permalink
util: python: is_forward_ref_dataclass: Helper to check for ForwardRe…
Browse files Browse the repository at this point in the history
…f or str

Related: python/cpython#21553 (comment)
Signed-off-by: John Andersen <johnandersenpdx@gmail.com>
  • Loading branch information
pdxjohnny committed Jan 5, 2023
1 parent a3a99fe commit 8fcff78
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dffml/util/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ def within_method(obj: object, method_name: str, max_depth: int = -1) -> bool:
return False


def is_forward_ref_dataclass(dataclass, type_cls):
"""
Check if a field's type is a ForwardRef, either via being an instance, or
being a type which is a string. An instance of a string is not a type,
therefore if we see a string, we should assume it is a ForwardRef.
"""
return isinstance(type_cls, (ForwardRef, str))


def resolve_forward_ref_dataclass(dataclass, type_cls):
"""
>>> import dataclasses
Expand Down

0 comments on commit 8fcff78

Please sign in to comment.