[Python-Dev] Question about PEP 484
Gregory P. Smith
greg at krypto.org
Mon Jul 16 17:56:38 EDT 2018
On Mon, Jul 16, 2018 at 1:44 PM Guido van Rossum <guido at python.org> wrote:
> As one of the authors of PEP 484, *I* never thought there was an ambiguity
> here. The intention was for stub files to conform to the same grammar as
> regular .py files, but with a different interpretation.
>
> > "Have the same syntax as regular Python modules" and "are files
> containing type hints" are at odds with each other.
>
> That depends. *same syntax as regular Python* is normative while
> *containing type hints* is an informal description of intent.
>
> I happen to be somewhat familiar with the situation that lead to this
> question -- pytype has its own parser for stub files that cannot parse all
> Python constructs. But claiming that PEP 484 is ambiguous feels wrong, and
> if we really need to clarify it the only way to go is to make "same syntax
> as regular Python" more clearly normative. Type checkers should of course
> feel free ignore everything they don't care about.
>
I feel like the "same syntax as regular Python" is too broad a statement.
That effectively requires a version specific Python interpreter to execute
the files. With at least four different Python static analyzers in
existence today, keeping the behavior of all of them consistent is
important. Otherwise pyi files will be (are being) created that are
analyzer specific and break other type checkers when distributed.
ex: We're encountering pyi files with conditional logic in them. I believe
we've encountered pyi files with del statements in them? Both of these are
a slippery slope towards being turing complete in something that isn't
supposed to be code. I don't like this. Interface declarations should not
contain logic. If we allow conditions, we need to explicitly define what
we do allow in the PEP. (if+else and del? what inputs are allowed for the
expression in if statements?). Otherwise at some point someone is going to
create a pyi file containing loops, function calls, and generator
expressions and expect it to _do_ something. The horror! Lets avoid that.
PEP-484 does contain the text, "This also reinforces the notion that no
runtime behavior should be expected of stub files." But reinforcing a
notion is not what I read as a concrete statement.
I'd rather see that say something like, "There must not be any runtime
behavior from a stub file. They will be parsed for information, not
executed." Wordsmith that all you want, I'm not pedantic enough. :)
I expect someone pedantic to happily point out that a def or class or
assignment to ... with an annotation is runtime behavior... technically
correct, but that isn't how people like me think of them in this context.
We use a Pythonic syntax for stubs to be consistent with the language, that
doesn't mean they are code.
I wrote more than I thought I would here, I'll stop now. :)
-gps
>
> Regarding the unicode issue, that is indeed unfortunate, and there's a
> long but inconclusive discussion at
> https://github.com/python/typing/issues/208. (If you want a longer
> discussion here please start a new subject.)
>
> On Mon, Jul 16, 2018 at 1:02 PM, Adam Cataldo via Python-Dev <
> python-dev at python.org> wrote:
>
>> Thanks Brett and Teddy,
>>
>> Just so it doesn't get lost in the shuffle as folks dive into details,
>> I'll re-ask my earlier question about stub files. Assuming there is
>> consensus that there is ambiguity to resolve in the current definition, is
>> updating the section on stub files the preferred option? The only
>> alternative I can think of is to pull this out into a separate PEP. I
>> frankly have no opinion on what the best way to capture this is. We're
>> happy to help out either way.
>>
>> On Mon, Jul 16, 2018 at 12:48 PM Teddy Sudol <tsudol at google.com> wrote:
>>
>>> Hi, my name is Teddy Sudol. I work with Adam and Rebecca on pytype.
>>>
>>> The explanation of stub files is unclear. The section you linked starts
>>> with, "Stub files are files containing type hints that are only for use by
>>> the type checker, not at runtime." According to
>>> https://www.python.org/dev/peps/pep-0484/#acceptable-type-hints, type
>>> hints may be classes, abstract base classes, types defined in the `types`
>>> and `typing` modules, type variables, type aliases and None. Further in the
>>> section you linked, PEP 484 also states, "Stub files have the same syntax
>>> as regular Python modules," and, "no runtime behavior should be expected of
>>> stub files."
>>>
>>> "Have the same syntax as regular Python modules" and "are files
>>> containing type hints" are at odds with each other. This has led to
>>> compatibility issues between Mypy and pytype. For example, `b''` is not a
>>> valid type annotation, but until a month ago, `codecs.pyi` in typeshed used
>>> exactly that:
>>> https://github.com/python/typeshed/commit/6bbf3d89eb9b6c3fd5b0c0f632b2ad9258cecf15#diff-5f6f48c425bc0c283784cf5277880c0cL95.
>>> If statements can be useful for things like version checks, but on the
>>> other hand, pyi files aren't supposed to have any runtime behavior.
>>> Additionally, codifying the syntax for pyi files would settle questions
>>> like whether constants should be typed using "x: <type hint>" or "x = ...
>>> # type: <type hint>".
>>>
>>> We would like to see a clear statement about the syntax of stub files.
>>> Personally, I think they should be a subset of Python, but I'd also be
>>> happy with an EBNF grammar for them.
>>>
>>> -- Teddy
>>>
>>>
>>> On Mon, Jul 16, 2018 at 11:05 AM Brett Cannon <brett at python.org> wrote:
>>>
>>>>
>>>>
>>>> On Mon, 16 Jul 2018 at 10:32 Adam Cataldo via Python-Dev <
>>>> python-dev at python.org> wrote:
>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> *Hi Folks,Cc: Rebecca, pytypeThis is Adam Cataldo; I’m the engineering
>>>>> manager for the Python team at Google. Rebecca Chen, our lead pytype
>>>>> <https://github.com/google/pytype> contributor, and I are interested in
>>>>> helping finalize PEP 484 if possible. To that end, we wanted to find out
>>>>> what technical issues the PEP 484 authors feel they still need to finalize.
>>>>> We also wanted to know how we can help.We have a large Python code base at
>>>>> Google, and may be able to use this to help resolve current incomplete
>>>>> definitions, by collecting data on how types are used. We also have a
>>>>> couple ambiguities that we’d love to get closure on: - One thing we care
>>>>> about in particular, given the implementation of pytype, is the detailed
>>>>> definition of what goes in a .pyi file. Do folks think this makes sense to
>>>>> include as part of PEP 484, or would this be better in a separate PEP? We’d
>>>>> love to get your thoughts.*
>>>>>
>>>>
>>>> What specifically do you want beyond
>>>> https://www.python.org/dev/peps/pep-0484/#stub-files?
>>>>
>>>>
>>>>>
>>>>>
>>>>>
>>>>> * - The relationship between unicode and typing.Text in Python 2 has
>>>>> been a recurring source of confusion for our users. Especially since we
>>>>> contributed <https://github.com/python/peps/pull/302> to the current state
>>>>> of affairs, we’d like to figure out how to establish clarity here.Thoughts?*
>>>>>
>>>>
>>>> Do be aware, Adam, that due to Guido's retirement last week people
>>>> might be a bit preoccupied and so a little slow in responding. But then
>>>> again Guido just got a bit more free time so he might chime in on this one.
>>>> ;)
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "pytype" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to pytype+unsubscribe at googlegroups.com.
>>>> To post to this group, send email to pytype at googlegroups.com.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/pytype/CAP1%3D2W4NxcsSdsiMrh55KhjkwgD0PGRcZJF_Azq3g6QFQ2oiAw%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/pytype/CAP1%3D2W4NxcsSdsiMrh55KhjkwgD0PGRcZJF_Azq3g6QFQ2oiAw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>> _______________________________________________
>> Python-Dev mailing list
>> Python-Dev at python.org
>> https://mail.python.org/mailman/listinfo/python-dev
>>
> Unsubscribe:
>> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>>
>>
>
>
> --
> --Guido van Rossum (python.org/~guido)
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/greg%40krypto.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180716/5e33f300/attachment-0001.html>
More information about the Python-Dev
mailing list