[Python-ideas] Optional static typing -- the crossroads
Petr Viktorin
encukou at gmail.com
Fri Aug 15 19:11:38 CEST 2014
On Fri, Aug 15, 2014 at 6:43 PM, Dennis Brakhane
<brakhane at googlemail.com> wrote:
> On 15.08.2014 17:42, Petr Viktorin wrote:
>>
>> The common use is not all that concise:
>> def foo(bar: int | None=None): pass
>>
>> Or alternatively it could be:
>> def foo(bar: int=None): pass
>> if the default was automatically allowed.
> (Assuming you mean "the type of the default")
I really meant *only* the default. This really only works for None,
but that's a good thing, since something like:
def foo(bar:int=''): pass
looks very suspicious. I'd be fine with the linter complaining about
foo('hello').
Of course you can always do:
def foo(bar: (int | str)=''): pass
> While I like the second form a bit more, it kinda goes against "explicit
> is better than implicit".
>
> Also, if I change the default value from None to 42, I've either changed
> the allowable types,
> or need to remember to turn "bar: int=None" into "bar: int|None = 42".
>
> Furthermore, what should happen in the following case:
>
>
> # no annotations here
> def foo(): ...
>
> def bar(evil: int = foo()): ...
>
>
> Should this be disallowed, as the type checker will not be able to know
> what type foo is? Should it just assume int?
> And in the latter case, what should happen if foo now gains a "-> float"
> annotation?
If your linter can't figure it out, just specify the default's type
explicitly. Always a good thing to do when something's not immediately
obvious.
More information about the Python-ideas
mailing list