[Python-ideas] Optional static typing -- the crossroads
Łukasz Langa
lukasz at langa.pl
Fri Aug 15 21:24:10 CEST 2014
On Aug 15, 2014, at 8:17 AM, Guido van Rossum <guido at python.org> wrote:
> You can probably come up with a notation for first-class variable annotations, e.g.
>
> x: Sequence[int] = []
Yes, that syntax is out of scope for now, though, right? If I understand your reasoning behind choosing Mypy’s function annotation syntax, we don’t want to create programs that require Python 3.5+ just to be parsed.
If we were to introduce first-class variable typing, yes, the syntax you propose is what I also had in mind.
> The value might be optional. The question is though, would the type (Sequence[int]) be stored anyway? Also, in a class body, does it define a class var or an instance var (or doesn't it matter?).
I wouldn’t change the current behaviour:
class C:
cls_member: str = ‘on the class’
def __init__(self):
self.obj_member: str = ‘on the instance'
self.cls_member = 2 # that’s the real question: type error or an instance member?
For that last case, even though it’s currently valid Python, my intuition tells me for Mypy to treat it as an error.
> Does this need a 'var' keyword to be ambiguous?
I fail to see any additional value provided by such keyword. What would stop people from doing
var i = 1
I don’t think we want to end up with that.
> I propose to disallow declaring multiple variables in this style, since it's hard to decide whether the comma should bind tighter than the '=' sign (as in assignments) or less tight (as in function headings).
Right. I wonder if we even need this. For lines that use multiple assignment just for brevity, they can switch to multiple lines for typing. For common cases like:
host, port = origin.rsplit(‘:’, 1)
successful, errors = query_the_world(hostnames)
I think the types can be easily inferred (assuming rsplit and query_the_world are annotated).
> While we’re at slaying dragons, I’ll also silently make str non-iterable so that we can use Sequence[str] meaningfully from now on… How about that?
>
> I hope you meant that as a joke. We missed our chance for that one with Python 3.0. We must live with it.
Yes, that was obviously just a joke. By the way, is the PEP number 4000 free? Asking for a friend.
--
Best regards,
Łukasz Langa
WWW: http://lukasz.langa.pl/
Twitter: @llanga
IRC: ambv on #python-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140815/62fdb37d/attachment.html>
More information about the Python-ideas
mailing list