Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Pilcher Newsgroups: comp.lang.python Subject: Any way to "subclass" typing.Annotated? Date: Tue, 28 Jan 2025 16:02:47 -0600 Lines: 44 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de EQSkOlGDtn2XNCl3o9oReATGykuOFmj0uSK16ScEA2IQ== Cancel-Lock: sha1:+YcEvE1q5rtkh6sHSXDwqZ7Rr0g= sha256:GYuLxpTJBcTFd94xP9nCMH5RmxVD3UTQe3SlyqnYw+s= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=gmail.com header.i=@gmail.com header.b=gp6Ioigj; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'this:': 0.03; 'bunch': 0.05; "python's": 0.05; 'example:': 0.09; "shouldn't": 0.09; 'far,': 0.16; 'intuitive': 0.16; 'received:mail- ot1-x334.google.com': 0.16; 'responses,': 0.16; 'somewhat': 0.16; 'subject:way': 0.16; 'variables,': 0.16; 'probably': 0.17; "can't": 0.17; 'message-id:@gmail.com': 0.18; 'to:addr:python- list': 0.20; "i've": 0.22; "i'd": 0.24; 'to:name:python- list@python.org': 0.24; 'anyone': 0.25; 'classes': 0.26; 'interface': 0.26; "isn't": 0.27; 'header:User-Agent:1': 0.30; 'disabled': 0.32; 'but': 0.32; "i'm": 0.33; 'received:google.com': 0.34; 'able': 0.34; 'from:addr:gmail.com': 0.34; 'particularly': 0.35; 'question.': 0.35; '...': 0.37; 'currently': 0.37; 'using': 0.37; "it's": 0.37; 'could': 0.37; 'class': 0.37; 'way': 0.38; 'read': 0.38; 'list': 0.39; 'this,': 0.39; 'something': 0.40; 'simply': 0.63; 'copy': 0.63; 'skip:m 20': 0.63; 'delivery': 0.64; 'your': 0.64; 'involve': 0.64; 'spend': 0.64; 'thus': 0.64; 'respond': 0.68; 'types,': 0.69; 'free': 0.71; 'background,': 0.84; 'skip:= 70': 0.84; 'subject:Any': 0.84; 'type,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738101770; x=1738706570; darn=python.org; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=sYa9lA7QYFF+6RTeg4f+Kxc6XAL6Z0jSvGUCVf1/wCE=; b=gp6IoigjFL0ZJSmfnXVqyWsyNFejTl1uJYiucXIUIgemxXqEB8ZObYSwyZBwttuJXM tsP6JnveiqvhHl6Smsmw8FYsWCs0jkHlLTW/DxaEffOEMSt9VjNUjQmuCcAndtkbQIJd qMW8rN9ESQpmMBu1IsDs9xTg61pVzR5rUoMYUmg2EkDRx2yyj8HlbQI82X131zGA1HV+ 3MOR7/w4Eu8jul1sOYfs8v46FXKdimAJd3JL2EFDY9UUSL5kC3P17GYYaLaOA7cQv9bT jvai+Cx3NXjhAxzwYdNsQHWv+vgZC+dOH6pDPgiIGLmQHdWEBquAUl9eb8B3D2rp4lKK ANmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738101770; x=1738706570; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=sYa9lA7QYFF+6RTeg4f+Kxc6XAL6Z0jSvGUCVf1/wCE=; b=vq5nBuTCTZSBGNntBDghkhB37FWnkV6xY0F+yJTSs/YfUYCP00OpARwLjYxVbfsrXo dn2zOmY42obcTYuWVlfvRMh74lZ63YOU3CQkmfzaY/J5ITLyjHrMBZ2WM2Uri3DqZoYc DtE8z/qB2AWAeV2qZOvDWvGzWGxDu9ur8h6Dtwdk4pY7Qy4F81ik5Q4ZsC1xPGNwnul5 lWXUL01iBmRZzLiZgRa/hMhtpXflNLMe+Zq4YQD9MVSM/zYg2RNb1aS8RweLa11MhR4N /sUaEeJGylsSatP8l4E5MKCEyuf2cCuQITm/mhjIIDI4/E77o6l99ILpy0fhU+3YjoNt wC5g== X-Gm-Message-State: AOJu0YxCf+ZILycOKsE7pqUvNL82cOqiu/xICPsKxQHYL81npQP8f+sK wPPfWiaiHRxCvhq8qYGkjpM+Qbg8OWnwXgtcoex/PI1wTQJ7DUR7LmrPcA== X-Gm-Gg: ASbGnctHmqUBWn6TTo0Rg8hyFXoNaFpzRzQnq00zxMCl26pMu2nPaSAkOeOC0O7Kj4K JAuL63El7fZ6x19YIfbmLxmZVGdig0KI/YroTrkplG6sAERzNhsIJp2r5CqOU9yu7rSdE/jIUsE gQkg6D9xAQcHQzF5Ym4y8FYUjgnkzdm2td449O9HzV4pBGoJd57BQAuW8HNL+YKMW3jcCOxVnkW 2EG98yeqDrK2xEae5pEVsggVqpCQwxtuGgB8jubS++CPfoFrIxRY63SB15Bs6eujXfjN2x+RuO2 +kcwPPrpJ3REZkd7P9YbfWQyxZWefENLY+Z6KiSm9vBAGuxEOUaJsiWjQpdT X-Google-Smtp-Source: AGHT+IEpB+MaYB8qBxPIFMzpRkjlQL0tiFo4xKpKdsxgTVMPzqA3UREAvlXd479qWpNdaN9RAtpBqw== X-Received: by 2002:a05:6871:a908:b0:29e:24c7:2861 with SMTP id 586e51a60fabf-2b32f01672bmr529299fac.13.1738101770149; Tue, 28 Jan 2025 14:02:50 -0800 (PST) User-Agent: Mozilla Thunderbird Content-Language: en-US X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Bytes: 6733 (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 ========================================================================