Closed sobolevn closed 6 years ago
Do you mean annotations?
Something like this:
def function(num: float) -> int:
pass
Right?
Yes.
I thinks that we can add annotations. What about Python 3.4?
Seems like annotations was added in Python 3.0. And we lose nothing.
@sobolevn Soooo... What shall we do? Refusing to support Python 3.4 doesn't seem to be a good idea.
It actually is in some cases. Since upgrading from 3.4
to 3.5
is painless in 90% of situations.
@sobolevn Let's do this!
Please, note that variable annotations is not supported in 3.5
. https://stackoverflow.com/questions/39971929/what-are-variable-annotations-in-python-3-6/39973133#39973133
So, we have to annotate methods and functions only.
@sobolevn I will add annotations for functions and methods. Can i hope for your help with situations which confuse me?
Example:
def randints(self, amount: int = None, a: int = 1, b: int = 100) -> list:
"""Generate list of random integers.
:param amount: Amount of elements.
:param a: Minimum value of range.
:param b: Maximum value of range.
:return: List of random integers.
"""
if not amount:
amount = 3
return [self.randint(a, b)
for _ in range(amount)]
Is it right that amout: int = None
. Is it should be integer default value or None
is okay?
@sobolevn How to be if the function returns multiple types using? Should we use typing
module?
In regular python
without types I would say that None
is perfectly fine.
But in this particular situation it is possible to change amount=None
to amount: int = 3
. That what it does anyway, doesn't it?
About function returning multiple types. That's a code smell for me. It is error prone for many reasons:
tuple
but list
is returned, seems like not a big deal, but our old code x = list_not_tuple + (1, 2, )
will failNone
- it is a disaster, since we will got TypeError
s with a 100% guaranteeSo, consider not to do it.
@sobolevn Problem is in standard python library. json
module returns both list
and dict
.
So, seems like i have found a solution.
With the provided solution there are a lot of uncovered cases, which are valid json
:
[1, [], {}, "z"]
1
and "a"
@lk-geimfari I've already mentioned this in #267, but if one is defaulting the argument to None
, which is automatically pushed to type(None)
or NoneType
, then we should use typing.Optional[the_type]
. In the example above, we'd have
def randints(self, amount: Optional[int] = None, a: int = 1, b: int = 100) -> list:
"""Generate list of random integers.
:param amount: Amount of elements.
:param a: Minimum value of range.
:param b: Maximum value of range.
:return: List of random integers.
"""
if amount is None:
amount = 3
return [self.randint(a, b) for _ in range(amount)]
By the way, I believe I mistakenly reversed the logic of that predicate in my PR earlier :P
@bjd2385 I'll update code using Optional
.
Sooo, it's done!
I have started to use
mypy
. Aaaand it is not so good right now.Libraries lack typing support. And I know that
mimesis
support onlypython3
. Do you consider adding type hints?There are some advantages:
There are some disadvantages:
python3.5
, so any version prior to that will have to use special comments or other hacks