[Python-ideas] Conventions for function annotations
Eric Snow
ericsnowcurrently at gmail.com
Tue Dec 4 19:22:46 CET 2012
Check out http://www.artima.com/weblogs/viewpost.jsp?thread=89161
-eric
On Tue, Dec 4, 2012 at 9:43 AM, Jasper St. Pierre <jstpierre at mecheye.net> wrote:
> Indeed. I've looked at annotations before, but I never understood the
> purpose. It seemed like a feature that was designed and implemented without
> some goal in mind, and where the community was supposed to discover the goal
> themselves.
>
> So, if I may ask, what was the original goal of annotations? The PEP gives
> some suggestions, but doesn't leave anything concrete. Was it designed to be
> an aid to IDEs, or static analysis tools that inspect source code? Something
> for applications themselves to munge through to provide special behaviors,
> like a command line parser, or runtime static checker?
>
> The local decorator influence might work, but that has the problem of only
> being able to be used once before we fall back to the old method. Would you
> rather:
>
> @tab_expand(filename=glob('*.txt'))
> @types
> def read_from_filename(filename:str, num_bytes:int) -> bytes:
> pass
>
> or
>
> @tab_expand(filename=glob('*.txt'))
> @types(filename=str, num_bytes=int, return_=bytes)
> def read_from_filename(filename, num_bytes):
> pass
>
> For consistency's sake, I'd prefer the latter.
>
> Note that we could take a convention, like Thomas suggests, and adopt both:
>
> @tab_expand
> @types
> def read_from_filename(filename:(str, glob('*.txt')), num_bytes:int) ->
> bytes:
> pass
>
> But that's a "worst of both worlds" approach: we lose the locality of which
> argument applies to which decorator (unless we make up rules about
> positioning in the tuple or something), and we gunk up the function
> signature, all to use a fancy new Python 3 feature.
>
> With a restricted and narrow focus, I could see them gaining adoption, but
> for now, it seems like extra syntax was introduced simply for the point of
> having extra syntax.
>
>
>
> On Sun, Dec 2, 2012 at 5:23 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> On 02/12/12 22:43, Nick Coghlan wrote:
>>
>>> Last time it came up, the collective opinion on python-dev was still to
>>> leave PEP 8 officially neutral on the topic so that people could
>>> experiment
>>> more freely with annotations and the community could help figure out what
>>> worked well and what didn't. Admittedly this was long enough ago that I
>>> don't remember the details, just the obvious consequence that PEP 8
>>> remains
>>> largely silent on the matter, aside from declaring that function
>>> annotations are off-limits for standard library modules: "The Python
>>> standard library will not use function annotations as that would result
>>> in
>>> a premature commitment to a particular annotation style. Instead, the
>>> annotations are left for users to discover and experiment with useful
>>> annotation styles."
>>
>>
>> I fear that this was a strategic mistake. The result, it seems to me, is
>> that
>> annotations have been badly neglected.
>>
>> I can't speak for others, but I heavily use the standard library as a
>> guide
>> to what counts as good practice in Python. I'm not a big user of third
>> party
>> libraries, and most of those are for 2.x, so with the lack of annotations
>> in
>> the std lib I've had no guidance as to what sort of things annotations
>> could
>> be used for apart from "type checking".
>>
>> I'm sure that I'm not the only one.
>>
>>
>>
>> --
>> Steven
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>
>
>
>
> --
> Jasper
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
More information about the Python-ideas
mailing list