Any way to "subclass" typing.Annotated?

Ian Pilcher arequipeno at gmail.com
Tue Jan 28 17:02:47 EST 2025


(Note:  I have mail delivery disabled for this list and read it through
GMane.  Please copy me on any responses, so that I can respond with
proper threading.)

 From the things that I probably shouldn't spend my free time on
department ...

As background, I'm working on a project that is going to involve a bunch
of abstract classes and dynamic types, and I've found that Python's
existing abstract class implementation leaves a lot to be desired,
particularly the inability to create abstract class variables and class
methods.  Having been seduced by the Siren song of Python's flexibility,
I've been rolling my own implementation.

Now to my question.

I'm currently using annotations to create abstract class variables, for
example:

   class Foo(object, metaclass=AbstractType):

       acv: Annotated[int, abstract]

('abstract' is simply a unique "flag" object.)

This works just fine, but it's somewhat un-idiomatic.  What I'd like to
be able to do is create my own type, so that I could do something like
this:

   class Foo(object, metaclass=AbstractType):

       acv: AbstractClassVariable[int]

Essentially I'd like to create "subclass" of typing.Annotated that
always sets the metadata to 'abstract'.  Thus far, I haven't found a
way to do this, as typing.Annotated can't be subclassed.

Anyone have any ideas?

-- 
========================================================================
If your user interface is intuitive in retrospect ... it isn't intuitive
========================================================================



More information about the Python-list mailing list