Skip to content

Commit

Permalink
Merge pull request #1281 from sbrunner/fix-union
Browse files Browse the repository at this point in the history
Fix | between string
  • Loading branch information
sbrunner authored Dec 13, 2024
2 parents c43abf8 + 4ee0466 commit 0dca64a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions jsonschema_gentypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,23 @@ def __init__(self, sub_types: list[Type]) -> None:
"""
super().__init__(NativeType("Union"), sub_types)

def _required_union(self, python_version: tuple[int, ...]) -> bool:
if python_version < (3, 10):
return True
for sub_type in self.sub_types:
if sub_type.name(python_version).startswith('"'):
return True
return False

def depends_on(self, python_version: tuple[int, ...]) -> list[Type]:
"""Return the needed sub types."""
if python_version < (3, 10):
if self._required_union(python_version):
return super().depends_on(python_version)
return self.sub_types + super().depends_on(python_version)

def name(self, python_version: tuple[int, ...]) -> str:
"""Return what we need to use the type."""
if python_version < (3, 10):
if self._required_union(python_version):
return super().name(python_version)
return f"{' | '.join([sub_type.name(python_version) for sub_type in self.sub_types])}"

Expand Down Expand Up @@ -682,7 +690,7 @@ def definition(self, python_version: tuple[int, ...], line_length: Optional[int]
"""Return the type declaration."""
result = ["", ""]
if isinstance(self.constant, dict) and not self.constant:
dict_type = "Dict" if python_version < (3, 9) else "doct"
dict_type = "Dict" if python_version < (3, 9) else "dict"
result.append(f"{self._name}: {dict_type}[str, Any] = {repr(self.constant)}")
elif isinstance(self.constant, (dict, list)) and not self.constant:
list_type = "List" if python_version < (3, 9) else "list"
Expand Down

0 comments on commit 0dca64a

Please sign in to comment.