Pylance and @overload of functions #5387
-
Hi, I have some question regarding the handling of Consider the following example : from typing import overload
@overload
def my_func(arg1: int, arg2: str = ...) -> None:
pass
@overload
def my_func(arg1: str, arg2: str = ...) -> str:
pass
def my_func(arg1: int | str, arg2: str = "default_value") -> None | str:
"""Function docstring"""
print(arg2)
if isinstance(arg1, int):
return None
else:
return arg1
def my_second_func(b: int | str) -> None | str:
my_value = my_func(b)
return my_value
int_value = 0
my_value = my_func(int_value)
reveal_type(my_value) First question : When I point the cursor to the list line, I get the right signature (the second overload) and the docstring from the function implementation, which is great ! However I don't get the default value of Second question : I am in a situation where I have many different overloads, with lots of parameters, something similar to https://discuss.python.org/t/make-overload-less-verbose/38803 . Is it possible to put all these overloads in a dedicated file to improve readability ? I do believe that you can use a test.pyi from typing import overload
@overload
def my_func(arg1: int, arg2: str = ...) -> None:
pass
@overload
def my_func(arg1: str, arg2: str = ...) -> str:
pass
@overload
def my_func(arg1: int | float, arg2: str = "default_value") -> None | float:
pass test.py def my_func(arg1: int | str, arg2: str = "default_value") -> None | str:
"""Function docstring"""
print(arg2)
if isinstance(arg1, int):
return None
else:
return arg1
int_value = 0
my_value = my_func(int_value)
reveal_type(my_value) my_value type is inferred to be If you have any insight on either of these issue, your help is much appreciated 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You may include default argument values in an overload signature if you'd like the value to be displayed in hover text and signature help. Default arguments are often replaced by
Stub files are stand-ins for a source file within a library. Stubs are typically written because the source file is untyped or because the module is written in a language other than Python. A type checker will use a stub in place of the source file when an import statement targets that source file. If you have a source file named
This tends to happen when you write functions and methods that are highly polymorphic. If you have the option of changing your interface, you might consider ways to make it less polymorphic. This would reduce the need to write such complex overloads. |
Beta Was this translation helpful? Give feedback.
You may include default argument values in an overload signature if you'd like the value to be displayed in hover text and signature help. Default arguments are often replaced by
...
in type stubs, but that's a convention more than a requirement. Type checkers and other static analysis tools will handle it fine if you include the actual value. Typeshed stubs are increasingly including the default argument value to improve language server support.Stub files are stand-ins for a source file within a library. Stubs are typically written because the source file is untyped or because…