Skip to content
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions simtypes/from_string.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)):
Expand Down Expand Up @@ -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'
Expand Down
14 changes: 14 additions & 0 deletions tests/units/test_from_string.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from math import inf, isnan
from typing import Any

import pytest
from full_match import match
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Loading