diff --git a/README.md b/README.md index 148df37..b9b1716 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Downloads](https://static.pepy.tech/badge/simtypes)](https://pepy.tech/project/simtypes) [![Coverage Status](https://coveralls.io/repos/github/pomponchik/simtypes/badge.svg?branch=main)](https://coveralls.io/github/pomponchik/simtypes?branch=main) [![Lines of code](https://sloc.xyz/github/pomponchik/simtypes/?category=code)](https://github.com/boyter/scc/) -[![Hits-of-Code](https://hitsofcode.com/github/pomponchik/simtypes?branch=main)](https://hitsofcode.com/github/pomponchik/simtypes/view?branch=main) +[![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) [![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) [![Python versions](https://img.shields.io/pypi/pyversions/simtypes.svg)](https://pypi.python.org/pypi/simtypes) [![PyPI version](https://badge.fury.io/py/simtypes.svg)](https://badge.fury.io/py/simtypes) @@ -191,6 +191,8 @@ print(from_string('-inf', float)) # strings print(from_string('I am the danger', str)) #> "I am the danger" +print(from_string('I am the danger', Any)) # Any is interpreted as a string. +#> "I am the danger" # bools print(from_string('yes', bool)) diff --git a/pyproject.toml b/pyproject.toml index 93bde3b..fb8b7c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "simtypes" -version = "0.0.5" +version = "0.0.6" authors = [{ name = "Evgeniy Blinov", email = "zheni-b@yandex.ru" }] description = 'Type checking in runtime without stupid games' readme = "README.md" diff --git a/simtypes/from_string.py b/simtypes/from_string.py index 7d2c4ac..e1751b7 100644 --- a/simtypes/from_string.py +++ b/simtypes/from_string.py @@ -1,4 +1,4 @@ -from typing import Type, get_origin +from typing import Type, Any, get_origin from json import loads, JSONDecodeError from inspect import isclass @@ -10,6 +10,9 @@ def from_string(value: str, expected_type: Type[ExpectedType]) -> ExpectedType: if not isinstance(value, str): raise ValueError(f'You can only pass a string as a string. You passed {type(value).__name__}.') + if expected_type is Any: + return value # type: ignore[return-value] + origin_type = get_origin(expected_type) 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: raise TypeError(f'The string "{value}" cannot be interpreted as an integer.') from e elif expected_type is float: - if value == '∞': + if value == '∞' or value == '+∞': value = 'inf' elif value == '-∞': value = '-inf' diff --git a/tests/units/test_from_string.py b/tests/units/test_from_string.py index 814fd2b..238b0c1 100644 --- a/tests/units/test_from_string.py +++ b/tests/units/test_from_string.py @@ -1,4 +1,5 @@ from math import inf, isnan +from typing import Any import pytest from full_match import match @@ -85,6 +86,7 @@ def test_get_float_value(): assert from_string('INF', float) == inf assert from_string('-INF', float) == -inf assert from_string('∞', float) == inf + assert from_string('+∞', float) == inf assert from_string('-∞', float) == -inf assert isnan(from_string('nan', float)) @@ -286,3 +288,15 @@ def test_get_dict_value(dict_type, subscribable_list_type, subscribable_dict_typ with pytest.raises(TypeError, match=match('The string "{"lol": {"kek": "kek"}}" cannot be interpreted as a dict of the specified format.')): from_string('{"lol": {"kek": "kek"}}', subscribable_dict_type[str, subscribable_dict_type[int, str]]) + + +@pytest.mark.parametrize( + ['string'], + [ + ('{"lol": "kek"}',), + ('1',), + ('kek',), + ], +) +def test_get_any(string): + assert from_string(string, Any) == string