I made this simple function which I want to check with mypy and pylint. It just parses a string and converts it to the appropriate type.
import re
from typing import Any, Callable
def parse_constant(constant: str) -> Any:
for reg, get_val in [
(re.compile(r'\'(.*)\''), str),
(re.compile(r'true', re.IGNORECASE), lambda _: True),
(re.compile(r'false', re.IGNORECASE), lambda _: False),
(re.compile(r'([0-9]+)'), int),
(re.compile(r'([0-9]+\.[0-9]+)'), float)
]:
match = reg.fullmatch(constant)
if match is not None:
if len(match.groups()) == 0:
val = None
else:
val = match.groups()[0]
return get_val(val)
return None
It works fine but mypy complains: I get error: "object" not callable
at line 18 (return get_val(val)
).
Now if I replace, str
by lambda x: str(x)
mypy is happy but pylint
complains with Lambda may not be necessary
.
What is the proper way to fix that?