Python type hinting dilemma: How to show bool in IDE while keeping Literal precision? /u/jtfidje Python Education

I’m working with function overloading in Python to provide precise type hints. Here’s a simplified version of my code:

from typing import overload, Any, Literal @overload def get_users(limit:int, skip: int, return_total_count: Literal[False]) -> list[Any]: ... u/overload def get_users(limit:int, skip: int, return_total_count: Literal[True]) -> tuple[list[Any], int]: ... def get_users(limit: int, skip: int, return_total_count: bool) -> list[Any] | tuple[list[Any], int]: ... users = get_users(10, 0, True) users = get_users(10, 0, False) 

This works as intended for type checking, correctly inferring the return types based on the return_total_count argument. However, when I hover over the function calls, my IDE shows:

(function) def get_users( limit: int, skip: int, return_total_count: Literal[True] ) -> tuple[list[Any], int] (function) def get_users( limit: int, skip: int, return_total_count: Literal[False] ) -> list[Any] 

While this is technically correct, I’d prefer to see bool instead of Literal[True] or Literal[False] for the return_total_count parameter in the type hint, while still maintaining the precise return type based on the True/False input.

Is there a way to achieve this? Or is this a limitation of Python’s type system and/or how IDEs display type hints for overloaded functions?

Any insights or alternative approaches would be greatly appreciated!

submitted by /u/jtfidje
[link] [comments]

​r/learnpython I’m working with function overloading in Python to provide precise type hints. Here’s a simplified version of my code: from typing import overload, Any, Literal @overload def get_users(limit:int, skip: int, return_total_count: Literal[False]) -> list[Any]: … u/overload def get_users(limit:int, skip: int, return_total_count: Literal[True]) -> tuple[list[Any], int]: … def get_users(limit: int, skip: int, return_total_count: bool) -> list[Any] | tuple[list[Any], int]: … users = get_users(10, 0, True) users = get_users(10, 0, False) This works as intended for type checking, correctly inferring the return types based on the return_total_count argument. However, when I hover over the function calls, my IDE shows: (function) def get_users( limit: int, skip: int, return_total_count: Literal[True] ) -> tuple[list[Any], int] (function) def get_users( limit: int, skip: int, return_total_count: Literal[False] ) -> list[Any] While this is technically correct, I’d prefer to see bool instead of Literal[True] or Literal[False] for the return_total_count parameter in the type hint, while still maintaining the precise return type based on the True/False input. Is there a way to achieve this? Or is this a limitation of Python’s type system and/or how IDEs display type hints for overloaded functions? Any insights or alternative approaches would be greatly appreciated! submitted by /u/jtfidje [link] [comments] 

I’m working with function overloading in Python to provide precise type hints. Here’s a simplified version of my code:

from typing import overload, Any, Literal @overload def get_users(limit:int, skip: int, return_total_count: Literal[False]) -> list[Any]: ... u/overload def get_users(limit:int, skip: int, return_total_count: Literal[True]) -> tuple[list[Any], int]: ... def get_users(limit: int, skip: int, return_total_count: bool) -> list[Any] | tuple[list[Any], int]: ... users = get_users(10, 0, True) users = get_users(10, 0, False) 

This works as intended for type checking, correctly inferring the return types based on the return_total_count argument. However, when I hover over the function calls, my IDE shows:

(function) def get_users( limit: int, skip: int, return_total_count: Literal[True] ) -> tuple[list[Any], int] (function) def get_users( limit: int, skip: int, return_total_count: Literal[False] ) -> list[Any] 

While this is technically correct, I’d prefer to see bool instead of Literal[True] or Literal[False] for the return_total_count parameter in the type hint, while still maintaining the precise return type based on the True/False input.

Is there a way to achieve this? Or is this a limitation of Python’s type system and/or how IDEs display type hints for overloaded functions?

Any insights or alternative approaches would be greatly appreciated!

submitted by /u/jtfidje
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *