On Thu, Dec 2, 2021 at 6:57 PM Stephan Richter <stephan.richter@gmail.com> wrote:
So maybe this is my time to chime in. I have used annotations for runtime
behavior. My primary use case is an injection library that I wrote.

Does it work with __future__ annotations?

-CHB



 
It allows
something along the lines of:

class IMyService(IService):
    pass

@inject
def call_something(arg1: str, arg2: int, svc: IMyService = None):
    assert svc is not None
    # do stuff...

At runtime the `inject()` decorator will scan the function signature and
insert an instance of `IMyService` based on a registry lookup. (We use
zope.component utilities, but that's an implementation detail.) When testing,
one can simply pass a mock version of the service.

Using the mypy-zope plugin, the above also passes all mypy type checking.

It is a simple pattern in terms of annotation but a very effective pattern
that mimics other injection systems in Java and TypeScript (Angular). We have
used it for a few years now and like it a lot.

Regards,
Stephan

On Monday, November 29, 2021 6:00:04 PM EST Barry Warsaw wrote:
> On Nov 26, 2021, at 01:13, Paul Moore <p.f.moore@gmail.com> wrote:
> > I'd therefore interpret Barry's plea as being for *anyone* with a use
> > for annotations to provide their feedback (at least, anyone who
> > accepts that annotations are types), with particular emphasis on
> > people who want to use the types declared in annotations to affect
> > runtime behaviour, as that's the most under-represented group at the
> > moment (and it's not clear whether it's under-represented because
> > there aren't many such uses, or because the users aren't being heard
> > from).
>
> Spot on.
>
> -Barry


--
Stephan Richter
Entrepreneur & Geek


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/XRJJ4ZMOQJXOTRFT3FQ25QXHXOKBVXKP/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython