Skip to content
Draft
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
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ toml = "^0.10.0"
megaparsy = "^0.1.4"
typesystem = "^0.2.4"
prompt-toolkit = "^3.0.3"
pydantic = "^1.6.1"
pydantic = "^2.0.0"
pydantic_settings = "^2.0.0"
parso = "^0.7.0,<0.8.0"
inject = "^4.3.1"
structlog = "^20.1.0"
Expand All @@ -44,5 +45,5 @@ pytype = "^2020.8.17"
waterloo = "waterloo.cli:main"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
2 changes: 1 addition & 1 deletion tests/refactor/test_annotations_pbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def identity(arg1):
with open(f.name, "w") as fw:
fw.write(content)

test_settings = settings.copy(deep=True)
test_settings = settings.model_copy(deep=True)
test_settings.ALLOW_UNTYPED_ARGS = False
test_settings.REQUIRE_RETURN_TYPE = False
test_settings.IMPORT_COLLISION_POLICY = import_collision_policy
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@inject.params(settings="settings")
def override_settings(settings, **kwargs):
test_settings = settings.copy(deep=True)
test_settings = settings.model_copy(deep=True)
for key, val in kwargs.items():
setattr(test_settings, key, val)
return test_settings
24 changes: 15 additions & 9 deletions waterloo/conf/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from enum import Enum
from typing import Dict, Optional, Union, no_type_check

from pydantic import BaseSettings, validator
from pydantic import field_validator
from pydantic_settings import SettingsConfigDict, BaseSettings

from waterloo.types import (
LOG_LEVEL_LABELS,
Expand All @@ -19,16 +20,19 @@ class CoerceEnumSettings(BaseSettings):

@no_type_check
def __setattr__(self, name, value):
field = self.__fields__[name]
if issubclass(field.type_, Enum) and not isinstance(value, Enum):
value = field.type_[value]
field = self.model_fields[name]
if issubclass(field.annotation, Enum) and not isinstance(value, Enum):
value = field.annotation[value]
return super().__setattr__(name, value)


class Settings(CoerceEnumSettings):
class Config:
validate_assignment = True
env_prefix = "WATERLOO_"
model_config = SettingsConfigDict(
validate_assignment=True,
env_prefix="WATERLOO_",
# TODO: replace all str field values strictly with str
coerce_numbers_to_str=True,
)

PYTHON_VERSION: str = "2.7"

Expand All @@ -43,15 +47,17 @@ class Config:
VERBOSE_ECHO: bool = True
LOG_LEVEL: LogLevel = LogLevel.INFO

@validator("IMPORT_COLLISION_POLICY")
@field_validator("IMPORT_COLLISION_POLICY")
@classmethod
def key_to_member(
cls, value: Union[ImportCollisionPolicy, str]
) -> ImportCollisionPolicy:
if isinstance(value, ImportCollisionPolicy):
return value
return ImportCollisionPolicy[value]

@validator("ECHO_STYLES")
@field_validator("ECHO_STYLES")
@classmethod
def echo_styles_required_fields(
cls, value: Optional[Dict[str, str]]
) -> Optional[Dict[str, str]]:
Expand Down
28 changes: 14 additions & 14 deletions waterloo/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ class Types(str, Enum):
NONE = "None"


class ImportStrategy(Enum):
USE_EXISTING = auto() # don't add any import, strip dotted path from docstring type
USE_EXISTING_DOTTED = auto() # don't add any import
ADD_FROM = auto() # from <dotted.package.path> import <name list>
ADD_DOTTED = auto() # import <dotted.package.path>


DOTTED_PATH_STRATEGIES: Final = {
ImportStrategy.ADD_DOTTED,
ImportStrategy.USE_EXISTING_DOTTED,
}


# https://sphinxcontrib-napoleon.readthedocs.io/en/latest/#docstring-sections
VALID_ARGS_SECTION_NAMES: Final = {
"Args",
Expand All @@ -42,7 +55,7 @@ class Types(str, Enum):
"Yields": (r"Yields", ReturnsSection.YIELDS),
}

NameToStrategy_T = Dict[str, "ImportStrategy"]
NameToStrategy_T = Dict[str, ImportStrategy]


class TypeAtom(NamedTuple):
Expand Down Expand Up @@ -324,19 +337,6 @@ def __len__(self):
return len(self.all_names)


class ImportStrategy(Enum):
USE_EXISTING = auto() # don't add any import, strip dotted path from docstring type
USE_EXISTING_DOTTED = auto() # don't add any import
ADD_FROM = auto() # from <dotted.package.path> import <name list>
ADD_DOTTED = auto() # import <dotted.package.path>


DOTTED_PATH_STRATEGIES: Final = {
ImportStrategy.ADD_DOTTED,
ImportStrategy.USE_EXISTING_DOTTED,
}


class AmbiguousTypeError(Exception):
settings = inject.attr("settings")

Expand Down