Skip to content

Commit 940408e

Browse files
authoredMay 20, 2024··
build: upgrade Pydantic to 2.7 (#241)
1 parent f869937 commit 940408e

File tree

8 files changed

+185
-169
lines changed

8 files changed

+185
-169
lines changed
 

‎foca/api/register_openapi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def register_openapi(
9090
spec.connexion = {} if spec.connexion is None else spec.connexion
9191
app.add_api(
9292
specification=spec_parsed,
93-
**spec.dict().get('connexion', {}),
93+
**spec.model_dump().get('connexion', {}),
9494
)
9595
logger.info(f"API endpoints added from spec: {spec.path_out}")
9696

‎foca/config/config_parser.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ def __init__(
6868
)
6969
if format_logs:
7070
self._configure_logging()
71-
logger.debug(f"Parsed config: {self.config.dict(by_alias=True)}")
71+
logger.debug(f"Parsed config: {self.config.model_dump(by_alias=True)}")
7272

7373
def _configure_logging(self) -> None:
7474
"""Configure logging."""
7575
try:
76-
dictConfig(self.config.log.dict(by_alias=True))
76+
dictConfig(self.config.log.model_dump(by_alias=True))
7777
except Exception as e:
78-
dictConfig(LogConfig().dict(by_alias=True))
78+
dictConfig(LogConfig().model_dump(by_alias=True))
7979
logger.warning(
8080
f"Failed to configure logging. Falling back to default "
8181
f"settings. Original error: {type(e).__name__}: {e}"

‎foca/models/config.py

+160-145
Large diffs are not rendered by default.

‎foca/security/access_control/register_access_control.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def register_permission_specs(
134134

135135
app.add_api(
136136
specification=spec.path[0], # type: ignore[index]
137-
**spec.dict().get("connexion", {}),
137+
**spec.model_dump().get("connexion", {}),
138138
)
139139
return app
140140

‎requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Flask~=2.2
66
flask-authz~=2.5
77
Flask-Cors~=4.0
88
Flask-PyMongo~=2.3
9-
pydantic~=1.10
9+
pydantic~=2.7
1010
PyJWT~=2.4
1111
pymongo~=4.7
1212
PyYAML~=6.0

‎requirements_dev.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ mypy>=0.991
55
mypy-extensions>=0.4.4
66
pylint>=3.2
77
pytest>=7.4
8-
python-semantic-release>=7.34,<8.2
8+
python-semantic-release>=9.7
99
types-PyYAML
1010
types-requests
1111
types-setuptools
1212
types-urllib3
13+
typing_extensions>=4.11

‎tests/config/test_config_parser.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,35 @@
3131

3232
def test_config_parser_valid_config_file():
3333
"""Test valid YAML parsing."""
34-
conf = ConfigParser(TEST_FILE)
35-
assert type(conf.config.dict()) == type(TEST_DICT)
34+
conf = ConfigParser(Path(TEST_FILE))
35+
assert type(conf.config.model_dump()) == type(TEST_DICT)
3636
assert isinstance(conf.config, type(TEST_CONFIG_INSTANCE))
3737

3838

3939
def test_config_parser_invalid_config_file():
4040
"""Test invalid YAML parsing."""
4141
with pytest.raises(ValidationError):
42-
ConfigParser(TEST_FILE_INVALID)
42+
ConfigParser(Path(TEST_FILE_INVALID))
4343

4444

4545
def test_config_parser_invalid_file_path():
4646
"""Test invalid file path."""
47-
conf = ConfigParser(TEST_FILE)
47+
conf = ConfigParser(Path(TEST_FILE))
4848
with pytest.raises(OSError):
49-
assert conf.parse_yaml("") is not None
49+
assert conf.parse_yaml(Path("")) is not None
5050

5151

5252
def test_config_parser_invalid_log_config():
5353
"""Test invalid log config YAML."""
54-
conf = ConfigParser(TEST_FILE_INVALID_LOG)
55-
assert type(conf.config.dict()) == type(TEST_DICT)
54+
conf = ConfigParser(Path(TEST_FILE_INVALID_LOG))
55+
assert type(conf.config.model_dump()) == type(TEST_DICT)
5656
assert isinstance(conf.config, type(TEST_CONFIG_INSTANCE))
5757

5858

5959
def test_config_parser_with_custom_config_model():
6060
"""Test with valid custom config model class."""
6161
conf = ConfigParser(
62-
config_file=TEST_FILE,
62+
config_file=Path(TEST_FILE),
6363
custom_config_model=TEST_CONFIG_MODEL,
6464
)
6565
assert isinstance(conf.config.custom.param, str)
@@ -68,22 +68,22 @@ def test_config_parser_with_custom_config_model():
6868

6969
def test_process_yaml_valid_config_file():
7070
"""Test process_yaml with valid YAML file."""
71-
result = ConfigParser.parse_yaml(TEST_FILE)
71+
result = ConfigParser.parse_yaml(Path(TEST_FILE))
7272
assert isinstance(result, dict)
7373

7474

7575
def test_process_yaml_invalid_config_file():
7676
"""Test process_yaml with invalid YAML file."""
7777
with pytest.raises(ValueError):
78-
ConfigParser.parse_yaml(TEST_FILE_INVALID_YAML)
78+
ConfigParser.parse_yaml(Path(TEST_FILE_INVALID_YAML))
7979

8080

8181
def test_process_yaml_missing_file():
8282
"""Test process_yaml when file cannot be opened."""
8383
with mock.patch("foca.config.config_parser.open") as mock_open:
8484
mock_open.side_effect = OSError
8585
with pytest.raises(OSError):
86-
ConfigParser.parse_yaml(TEST_FILE)
86+
ConfigParser.parse_yaml(Path(TEST_FILE))
8787

8888

8989
def test_merge_yaml_with_no_args():
@@ -95,14 +95,14 @@ def test_merge_yaml_with_no_args():
9595

9696
def test_merge_yaml_with_two_args():
9797
"""Test merge_yaml with no arguments."""
98-
yaml_list = [PATH, PATH_ADDITION]
98+
yaml_list = [Path(PATH), Path(PATH_ADDITION)]
9999
res = ConfigParser.merge_yaml(*yaml_list)
100100
assert 'put' in res['paths']['/pets/{petId}']
101101

102102

103103
def test_parse_custom_config_valid_model():
104104
"""Test ``.parse_custom_config()`` with a valid model class."""
105-
conf = ConfigParser(config_file=TEST_FILE)
105+
conf = ConfigParser(config_file=Path(TEST_FILE))
106106
result = conf.parse_custom_config(model=TEST_CONFIG_MODEL)
107107
assert isinstance(result, BaseModel)
108108
assert isinstance(result.param, str)
@@ -111,20 +111,20 @@ def test_parse_custom_config_valid_model():
111111

112112
def test_parse_custom_config_model_module_not_exists():
113113
"""Test ``.parse_custom_config()`` when module does not exist."""
114-
conf = ConfigParser(config_file=TEST_FILE)
114+
conf = ConfigParser(config_file=Path(TEST_FILE))
115115
with pytest.raises(ValueError):
116116
conf.parse_custom_config(model=TEST_CONFIG_MODEL_MODULE_NOT_EXISTS)
117117

118118

119119
def test_parse_custom_config_model_not_exists():
120120
"""Test ``.parse_custom_config()`` when model class does not exist."""
121-
conf = ConfigParser(config_file=TEST_FILE)
121+
conf = ConfigParser(config_file=Path(TEST_FILE))
122122
with pytest.raises(ValueError):
123123
conf.parse_custom_config(model=TEST_CONFIG_MODEL_NOT_EXISTS)
124124

125125

126126
def test_parse_custom_config_invalid():
127127
"""Test ``.parse_custom_config()`` when model class does not exist."""
128-
conf = ConfigParser(config_file=TEST_FILE_CUSTOM_INVALID)
128+
conf = ConfigParser(config_file=Path(TEST_FILE_CUSTOM_INVALID))
129129
with pytest.raises(ValueError):
130130
conf.parse_custom_config(model=TEST_CONFIG_MODEL)

‎tests/models/test_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
DIR = Path(__file__).parent / "test_files"
2020
EXCEPTIONS_NO_DICT = []
21-
EXCEPTIONS_NOT_NESTED = {'a': 'b'}
21+
EXCEPTIONS_NOT_NESTED = {Exception: 'b'}
2222
EXCEPTIONS_NOT_EXC = {'a': {'status': 400, 'title': 'Bad Request'}}
2323
REQUIRED_MEMBERS = [['title'], ['status']]
2424
MEMBER_TITLE = ['title']

0 commit comments

Comments
 (0)
Please sign in to comment.