[pytest-dev] possibly upsetting question on annotations

oliver oliver.schoenborn at gmail.com
Sat Mar 4 12:18:10 EST 2017


Hello, I love Python annotations and I use them in all my projects where
backwards compatiblity to 2.x is not an issue, really helps with
documenting intent (and code completion in PyCharm!).

But one thing that I find really puzzling is why CPython's "Annotations
engine" can't be fixed to support referencing a name before it is defined.
For example, this leads to a runtime error:

class Foo:
    def some_meth(self, other_foo: Foo):
        pass

This seems like a wart; having to use a string is just counter-intuitive
and ugly and takes away from Python's beauty, simplicity and cleanness. As
a workaround, we define our own decorator that allows us to mark a class as
a "forward declaration for annotation", like this:

@fwd_declaration
class Foo:
    pass

Then when the actual "class Foo" definition has Foo as annotation, the Foo
is the forward-declared one, keeps the interpreter happy. So it does the
job but because it is non-standard, IDE's don't know how to make use of
this information, and it is a bit annoying to have to write.

Is there really no way that this will *ever* be fixed so we don't need to
use strings or forward declaration hack?


-- 
Oliver
My StackOverflow contributions
My CodeProject articles
My Github projects
My SourceForget.net projects
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20170304/b06de02d/attachment.html>


More information about the pytest-dev mailing list