Any way to "subclass" typing.Annotated?

Fabien LUCE fabienluce at gmail.com
Fri Jan 31 05:41:22 EST 2025


Maybe you'd better use descriptors?

On Tue, 28 Jan 2025 at 23:03, Ian Pilcher via Python-list <
python-list at python.org> wrote:

> (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
> ========================================================================
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list