Skip to content

Commit 3d0b691

Browse files
authored
Merge pull request #6 from pomponchik/develop
0.0.6
2 parents c97f8ff + c29d44b commit 3d0b691

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Downloads](https://static.pepy.tech/badge/simtypes)](https://pepy.tech/project/simtypes)
55
[![Coverage Status](https://coveralls.io/repos/github/pomponchik/simtypes/badge.svg?branch=main)](https://coveralls.io/github/pomponchik/simtypes?branch=main)
66
[![Lines of code](https://sloc.xyz/github/pomponchik/simtypes/?category=code)](https://github.com/boyter/scc/)
7-
[![Hits-of-Code](https://hitsofcode.com/github/pomponchik/simtypes?branch=main)](https://hitsofcode.com/github/pomponchik/simtypes/view?branch=main)
7+
[![Hits-of-Code](https://hitsofcode.com/github/pomponchik/simtypes?branch=main&label=Hits-of-Code&exclude=docs/)](https://hitsofcode.com/github/pomponchik/simtypes/view?branch=main)
88
[![Test-Package](https://github.com/pomponchik/simtypes/actions/workflows/tests_and_coverage.yml/badge.svg)](https://github.com/pomponchik/simtypes/actions/workflows/tests_and_coverage.yml)
99
[![Python versions](https://img.shields.io/pypi/pyversions/simtypes.svg)](https://pypi.python.org/pypi/simtypes)
1010
[![PyPI version](https://badge.fury.io/py/simtypes.svg)](https://badge.fury.io/py/simtypes)
@@ -191,6 +191,8 @@ print(from_string('-inf', float))
191191
# strings
192192
print(from_string('I am the danger', str))
193193
#> "I am the danger"
194+
print(from_string('I am the danger', Any)) # Any is interpreted as a string.
195+
#> "I am the danger"
194196

195197
# bools
196198
print(from_string('yes', bool))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "simtypes"
7-
version = "0.0.5"
7+
version = "0.0.6"
88
authors = [{ name = "Evgeniy Blinov", email = "zheni-b@yandex.ru" }]
99
description = 'Type checking in runtime without stupid games'
1010
readme = "README.md"

simtypes/from_string.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type, get_origin
1+
from typing import Type, Any, get_origin
22
from json import loads, JSONDecodeError
33
from inspect import isclass
44

@@ -10,6 +10,9 @@ def from_string(value: str, expected_type: Type[ExpectedType]) -> ExpectedType:
1010
if not isinstance(value, str):
1111
raise ValueError(f'You can only pass a string as a string. You passed {type(value).__name__}.')
1212

13+
if expected_type is Any:
14+
return value # type: ignore[return-value]
15+
1316
origin_type = get_origin(expected_type)
1417

1518
if any(x in (dict, list, tuple) for x in (expected_type, origin_type)):
@@ -44,7 +47,7 @@ def from_string(value: str, expected_type: Type[ExpectedType]) -> ExpectedType:
4447
raise TypeError(f'The string "{value}" cannot be interpreted as an integer.') from e
4548

4649
elif expected_type is float:
47-
if value == '∞':
50+
if value == '∞' or value == '+∞':
4851
value = 'inf'
4952
elif value == '-∞':
5053
value = '-inf'

tests/units/test_from_string.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from math import inf, isnan
2+
from typing import Any
23

34
import pytest
45
from full_match import match
@@ -85,6 +86,7 @@ def test_get_float_value():
8586
assert from_string('INF', float) == inf
8687
assert from_string('-INF', float) == -inf
8788
assert from_string('∞', float) == inf
89+
assert from_string('+∞', float) == inf
8890
assert from_string('-∞', float) == -inf
8991

9092
assert isnan(from_string('nan', float))
@@ -286,3 +288,15 @@ def test_get_dict_value(dict_type, subscribable_list_type, subscribable_dict_typ
286288

287289
with pytest.raises(TypeError, match=match('The string "{"lol": {"kek": "kek"}}" cannot be interpreted as a dict of the specified format.')):
288290
from_string('{"lol": {"kek": "kek"}}', subscribable_dict_type[str, subscribable_dict_type[int, str]])
291+
292+
293+
@pytest.mark.parametrize(
294+
['string'],
295+
[
296+
('{"lol": "kek"}',),
297+
('1',),
298+
('kek',),
299+
],
300+
)
301+
def test_get_any(string):
302+
assert from_string(string, Any) == string

0 commit comments

Comments
 (0)