Skip to content

Commit 2abef0f

Browse files
authored
Merge pull request #342 from jacebrowning/release-2.3.1
Remove unnecessary eval calls
2 parents 51ad263 + 408a226 commit 2abef0f

File tree

9 files changed

+12
-32
lines changed

9 files changed

+12
-32
lines changed

.appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ install:
1818
- set PATH=C:\Python%PYTHON_MAJOR%%PYTHON_MINOR%\Scripts;%PATH%
1919
# Install system dependencies
2020
- choco install make
21+
- set POETRY_VERSION=1.8.5
2122
- curl -sSL https://install.python-poetry.org | python -
2223
- set PATH=%USERPROFILE%\AppData\Roaming\Python\Scripts;%PATH%
2324
- make doctor

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
python-version: ${{ matrix.python-version }}
2020

2121
- name: Install Poetry
22-
run: curl -sSL https://install.python-poetry.org | python3 -
22+
run: curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.8.5 python3 -
2323

2424
- name: Check dependencies
2525
run: make doctor

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
python 3.11.5
2-
poetry 1.8.2
2+
poetry 1.8.5

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## 2.3.1 (2025-02-11)
4+
5+
- Removed unnecessary `eval()` call to map annotations to types.
6+
37
## 2.3 (2024-10-30)
48

59
- Added support for the [JSON5](https://json5.org) file format.

datafiles/converters/__init__.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import dataclasses
2-
import inspect
32
import types
43
from enum import Enum
54
from inspect import isclass
@@ -38,18 +37,6 @@ def register(cls: Union[type, str], converter: type):
3837
register(dict, Dictionary)
3938

4039

41-
def resolve(annotation, obj=None):
42-
if isinstance(annotation, str):
43-
log.debug(f"Attempting to eval {annotation!r} using {obj}")
44-
annotation = annotation.replace("List", "list").replace("Dict", "list")
45-
namespace = inspect.getmodule(obj).__dict__ if obj else None
46-
try:
47-
return eval(annotation, namespace) # pylint: disable=eval-used
48-
except NameError as e:
49-
log.warn(f"Unable to eval: {e}")
50-
return annotation
51-
52-
5340
@cached
5441
def map_type(cls, *, name: str = "", item_cls: Optional[type] = None):
5542
"""Infer the converter type from a dataclass, type, or annotation."""
@@ -66,7 +53,7 @@ def map_type(cls, *, name: str = "", item_cls: Optional[type] = None):
6653
if dataclasses.is_dataclass(cls):
6754
converters = {}
6855
for field in dataclasses.fields(cls):
69-
converters[field.name] = map_type(resolve(field.type), name=field.name) # type: ignore
56+
converters[field.name] = map_type(field.type, name=field.name) # type: ignore
7057
converter = Dataclass.of_mappings(cls, converters)
7158
log.debug(f"Mapped {cls!r} to new converter: {converter}")
7259
return converter
@@ -79,8 +66,6 @@ def map_type(cls, *, name: str = "", item_cls: Optional[type] = None):
7966
converter = converter.as_optional()
8067
return converter
8168

82-
cls = resolve(cls)
83-
8469
if hasattr(cls, "__origin__"):
8570
converter = None
8671

datafiles/mapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from cached_property import cached_property
1414

1515
from . import config, formats, hooks
16-
from .converters import Converter, List, map_type, resolve
16+
from .converters import Converter, List, map_type
1717
from .types import Missing, Trilean
1818
from .utils import display, get_default_field_value, recursive_update, write
1919

@@ -301,7 +301,7 @@ def create_mapper(obj, root=None) -> Mapper:
301301
for field in [field for field in dataclasses.fields(obj) if field.init]:
302302
self_name = f"self.{field.name}"
303303
if pattern is None or self_name not in pattern:
304-
attrs[field.name] = map_type(resolve(field.type, obj), name=field.name) # type: ignore
304+
attrs[field.name] = map_type(field.type, name=field.name) # type: ignore
305305

306306
return Mapper(
307307
obj,

datafiles/tests/test_manager.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# pylint: disable=unused-variable,unused-argument
22

3-
import os
43
import shutil
54
from dataclasses import dataclass
65
from pathlib import Path
@@ -42,11 +41,6 @@ def manager(files: Path):
4241
model = create_model(MyClass, pattern="files/{self.foo}.yml")
4342
return Manager(model)
4443

45-
@pytest.fixture
46-
def manager_at_home():
47-
model = create_model(Nested, pattern="~/.{self.name}.json")
48-
return Manager(model)
49-
5044
@pytest.fixture
5145
def manager_with_files(files: Path):
5246
files.mkdir(exist_ok=True)
@@ -145,11 +139,6 @@ def when_no_files_exist(expect, manager: Manager):
145139
items = list(manager.all())
146140
expect(items) == []
147141

148-
def with_home_directory(expect, manager_at_home: Manager):
149-
items = list(manager_at_home.all())
150-
if "CI" not in os.environ:
151-
expect(len(items)) > 0
152-
153142
def describe_filter():
154143
@patch("datafiles.mapper.Mapper.exists", False)
155144
def when_no_files_exist(expect, manager: Manager):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22

33
name = "datafiles"
4-
version = "2.3"
4+
version = "2.3.1"
55
description = "File-based ORM for dataclasses."
66

77
license = "MIT"

tests/test_custom_converters_future.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from datafiles import datafile
1010

1111

12+
@pytest.mark.xfail(reason="https://github.com/jacebrowning/datafiles/issues/131")
1213
def test_optional_type(expect):
1314
@datafile("../tmp/sample.yml")
1415
class MyObject:

0 commit comments

Comments
 (0)