Inheriting from Union and Literal
Currently, it does not seem to be possible to inherit from Union or Literal. Would it make sense to provide this functionality? Use case: I would like to narrow down a type. I want to use the typing system as a safeguard that the restricted child type is actually smaller than the parent type. Here is an example: ```Python Currency: Literal['CHF', 'EUR', 'USD',] EuropeanCurrency: Literal['CHF', 'EUR'] FooCurrency: Literal['FOO', 'EUR'] # Should result in an error, as 'FOO' is not a Currency. ``` Has this been discussed before? --Jonathan
"Inheritance" is a concept that applies to class hierarchies. There's a similar concept in type systems called "subtyping". I think you're referring to the latter here. It wouldn't make sense to "inherit" from Union or Literal because those aren't types in the Python type system. They're special forms that allow you to express unions of types or literal types. Static type checkers like mypy, pyright or pyre already understand the subtyping rules for unions and literals. In your example, `EuropeanCurrency` is a subtype of `Currency`, but `FooCurrency` is not. Consider the following code: ```python from typing import Literal Currency = Literal['CHF', 'EUR', 'USD',] EuropeanCurrency = Literal['CHF', 'EUR'] FooCurrency = Literal['FOO', 'EUR'] def func(ec: EuropeanCurrency, fc: FooCurrency): x: Currency = ec y: Currency = fc # Error: FooCurrency is not compatible with Currency ``` -- Eric Traut Contributor to Pyright & Pylance Microsoft
Thanks, Erik, you are right - I meant "subtyping". I am looking for way to enforce that some literal type alias is a subtype of a certain other literal type. In my example, the typechecker should err on FooCurrency, because it is not a subtype of Currency. (Of course, for that, the subtype relation between FooCurrency and Currency would need to be expressed in some way). Am 16.05.2022 18:14 schrieb Eric Traut:
"Inheritance" is a concept that applies to class hierarchies. There's a similar concept in type systems called "subtyping". I think you're referring to the latter here. It wouldn't make sense to "inherit" from Union or Literal because those aren't types in the Python type system. They're special forms that allow you to express unions of types or literal types.
Static type checkers like mypy, pyright or pyre already understand the subtyping rules for unions and literals. In your example, `EuropeanCurrency` is a subtype of `Currency`, but `FooCurrency` is not.
Consider the following code:
```python from typing import Literal
Currency = Literal['CHF', 'EUR', 'USD',] EuropeanCurrency = Literal['CHF', 'EUR'] FooCurrency = Literal['FOO', 'EUR']
def func(ec: EuropeanCurrency, fc: FooCurrency): x: Currency = ec y: Currency = fc # Error: FooCurrency is not compatible with Currency ```
-- Eric Traut Contributor to Pyright & Pylance Microsoft _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: j.scholbach@posteo.de
El mar, 17 may 2022 a las 2:53,
Thanks, Erik, you are right - I meant "subtyping".
I am looking for way to enforce that some literal type alias is a subtype of a certain other literal type. In my example, the typechecker should err on FooCurrency, because it is not a subtype of Currency. (Of course, for that, the subtype relation between FooCurrency and Currency would need to be expressed in some way).
You can do this as with any other subtyping relationship, by creating a dummy function and passing your object to it. def needs_currency(c: Currency) -> None: ... def check(f: FooCurrency) -> None: needs_currency(f) # type checker error
Am 16.05.2022 18:14 schrieb Eric Traut:
"Inheritance" is a concept that applies to class hierarchies. There's a similar concept in type systems called "subtyping". I think you're referring to the latter here. It wouldn't make sense to "inherit" from Union or Literal because those aren't types in the Python type system. They're special forms that allow you to express unions of types or literal types.
Static type checkers like mypy, pyright or pyre already understand the subtyping rules for unions and literals. In your example, `EuropeanCurrency` is a subtype of `Currency`, but `FooCurrency` is not.
Consider the following code:
```python from typing import Literal
Currency = Literal['CHF', 'EUR', 'USD',] EuropeanCurrency = Literal['CHF', 'EUR'] FooCurrency = Literal['FOO', 'EUR']
def func(ec: EuropeanCurrency, fc: FooCurrency): x: Currency = ec y: Currency = fc # Error: FooCurrency is not compatible with Currency ```
-- Eric Traut Contributor to Pyright & Pylance Microsoft _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: j.scholbach@posteo.de
Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jelle.zijlstra@gmail.com
participants (3)
-
Eric Traut
-
j.scholbach@posteo.de
-
Jelle Zijlstra