You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I often wish that types could also be namespaces for functions. To motivate this idea, let me start with a simple example. Say I need some functions that apply to arbitrary 2-tuples. The simplest solution is to write a module defining a Pair type along with the functions I need:
This works and it typechecks with mypy. However, naming is a bit cumbersome with this approach. I can't use the pair identifier for Pair variables because I've already used it for my module, and I'm forced to resort to something like p instead. Also, it would be nicer if the functions fst and snd had a clearer association with the type Pair.
I'd much rather write something like this for my test:
Unfortunately, it does not typecheck with mypy, first of all because generic tuples are not allowed (#685). Second, even if generic tuples were supported, my assignment statement for pair would fail with an "Incompatible type" error because the expression has type Tuple[int, float] while the variable has type Pair[int, float], and mypy sees Pair as a subtype of Tuple due to the class definition.
I'm really curious to hear people's thoughts on this.
Thanks.
The text was updated successfully, but these errors were encountered:
I completely lost track of this. Apparently I was looking for a way to associate methods/functions with a type without creating a subclass, but it's been a long time since I opened this issue and I can't remember why. As far as I can tell, this still isn't really possible and my first example (type alias + independent functions) is the most straightforward workaround.
That aside, I'm happy to see that subclassing generic tuples now typechecks on master, I assume as of #13396. Full example:
I often wish that types could also be namespaces for functions. To motivate this idea, let me start with a simple example. Say I need some functions that apply to arbitrary 2-tuples. The simplest solution is to write a module defining a
Pair
type along with the functions I need:I would use my module like this:
This works and it typechecks with mypy. However, naming is a bit cumbersome with this approach. I can't use the
pair
identifier forPair
variables because I've already used it for my module, and I'm forced to resort to something likep
instead. Also, it would be nicer if the functionsfst
andsnd
had a clearer association with the typePair
.I'd much rather write something like this for my test:
where
Pair
is now both a type and a namespace for associated functions.Is it possible for mypy to support this?
I'm not making any syntax proposals here, but I'll note that the following does work at runtime:
Unfortunately, it does not typecheck with mypy, first of all because generic tuples are not allowed (#685). Second, even if generic tuples were supported, my assignment statement for
pair
would fail with an "Incompatible type" error because the expression has typeTuple[int, float]
while the variable has typePair[int, float]
, and mypy seesPair
as a subtype ofTuple
due to the class definition.I'm really curious to hear people's thoughts on this.
Thanks.
The text was updated successfully, but these errors were encountered: