Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new-style unions #58

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion configen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from enum import Enum
from pathlib import Path
from types import UnionType
from typing import (
Any,
List,
Expand Down Expand Up @@ -87,7 +88,10 @@ def is_tuple_annotation(type_: Any) -> bool:
def convert_imports(imports: Set[Type], string_imports: Set[str]) -> List[str]:
tmp = set()
for import_ in imports:
if import_ is Any:
if isinstance(import_, UnionType):
tmp.add("from typing import Union")
continue
elif import_ is Any:
classname = "Any"
elif import_ is Optional: # type: ignore
classname = "Optional"
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ license = "MIT License"
packages = [{ include = "configen" }]
include=["configen/py.typed"]
classifiers=[
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Typing :: Typed",
Expand All @@ -19,7 +19,7 @@ keywords=["hydra", "python"]
readme="README.md"

[tool.poetry.dependencies]
python = ">=3.9,<3.13"
python = ">=3.10,<3.13"
hydra-core = "^1.2.0"
jinja2 = "^3.0.3"
markupsafe = "*"
Expand All @@ -36,7 +36,7 @@ configen = 'configen.configen:main'

[tool.black]
line-length = 100
target-version = ['py39']
target-version = ['py310']
include = '\.pyi?$'
extend-exclude = "(hydra/grammar/gen|tools/configen/example/gen)"

Expand Down Expand Up @@ -73,7 +73,7 @@ classes = ["MISSING"]
include = ["configen"]
exclude = ["**/node_modules", "**/__pycache__"]
typeCheckingMode = "basic"
pythonVersion = "3.9"
pythonVersion = "3.10"
reportUnusedImport = "error"
reportDuplicateImport = "error"
reportIncompatibleVariableOverride = "error"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ def __init__(
param4: Union[str, DictConfig] = "",
param5: Union[str, List[str], Tuple[str, ...]] = ("foo", "bar"),
param6: Union[str, list[str], tuple[str, ...]] = ("foo", "bar"),
param7: str | Path = "",
):
self.param = param
self.param2 = param2
self.param3 = param3
self.param4 = param4
self.param5 = param5
self.param6 = param6
self.param7 = param7

def __eq__(self, other):
return (
Expand All @@ -105,6 +107,7 @@ def __eq__(self, other):
and other.param4 == self.param4
and other.param5 == self.param5
and other.param6 == self.param6
and other.param7 == self.param7
)


Expand Down
1 change: 1 addition & 0 deletions tests/test_modules/generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class UnionArgConf:
param4: Any = "" # Union[DictConfig, str]
param5: Any = ('foo', 'bar') # Union[List[str], Tuple[str, ...], str]
param6: Any = ('foo', 'bar') # Union[list[str], str, tuple[str, ...]]
param7: Union[Path, str] = ""


@dataclass
Expand Down
Loading