Python Forum
mypy, type aliases and type variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mypy, type aliases and type variables
#1
I'm not sure, whether it's a good place to ask such question. If it's not, please tell me, where it suits better.

I use Python 3.11/3.12, and mypy 1.6.0.

Here's the code:

from typing import TypeVar, Callable

T = TypeVar('T')

# type alias, for Python 3.11
IntToSomething = Callable[ [int], T ]

def callPlusOne(f : IntToSomething, val : int) -> T:
  return f(val+1)

def prettyInt(val : int) -> str:
  return f'Your pretty number is {val}'

print( callPlusOne(prettyInt, 10) )
It's perfectly fine, and can be run without any problems. mypy finds it incorrect, though (it's about the callPlusOne function):

Output:
error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
The solution (?) is to get rid of type alias (who needs them, anyway? Wink ):

from typing import TypeVar, Callable

T = TypeVar('T')

def callPlusOne(f : Callable[ [int], T ], val : int) -> T:
  return f(val+1)

def prettyInt(val : int) -> str:
  return f'Your pretty number is {val}'

print( callPlusOne(prettyInt, 10) )
No type alias used, no problem for mypy. And anyone is happy. Well, anyone but me: I'd like to use type aliases, since they make my code more readable.

Am I missing something, or mypy has some problems with type parameters in type aliases?
Reply
#2
The documentation for typing states:

Quote:NOTE: in Python 3.5 and later, the typing module lives in the stdlib, and installing this package has NO EFFECT, because stdlib takes higher precedence than the installation directory. To get a newer version of the typing module in Python 3.5 or later, you have to upgrade to a newer Python (bugfix) version. For example, typing in Python 3.6.0 is missing the definition of ‘Type’ – upgrading to 3.6.2 will fix this.

Since you are using python 3.11, it has no effect and therefore should not be imported.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python class members based on a type value voidtrance 7 1,295 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  type object 'man' has no attribute 'centerX' Tempo 7 878 Mar-07-2025, 03:47 AM
Last Post: deanhystad
Question how to type hint a function in a dataclass? Calab 3 883 Feb-27-2025, 04:40 AM
Last Post: Calab
  help with a script that adds docstrings and type hints to other scripts rickbunk 2 1,275 Feb-24-2025, 05:12 AM
Last Post: from1991
  Changing client.get() method type based on size of data... dl0dth 1 735 Jan-02-2025, 08:30 PM
Last Post: dl0dth
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 2,667 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  A question on Boolean Data Type Hudjefa 5 1,226 Aug-13-2024, 11:03 AM
Last Post: Hudjefa
  Precision with Decimal Type charlesrkiss 10 2,922 Aug-06-2024, 10:07 AM
Last Post: calculadora03
  How can I create this type hint. deanhystad 0 612 Aug-05-2024, 07:55 PM
Last Post: deanhystad
  How to type *_ deanhystad 0 594 Jun-12-2024, 10:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020