Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Overload function with SupportsInt and not SupportsInt?

$
0
0

I want to overload the function below so if passed a value that supports int() Python type hints int otherwise Python type hints the value passed.

Python typing module provides a SupportsInt type we can use to check if our value supports int.

from typing import Any, SupportsInt, overload@overloaddef to_int(value: SupportsInt) -> int: ...@overloaddef to_int[T: NotSupportsInt???](value: T) -> T: ...def to_int(value: Any) -> Any:    try:        return int(value)    except TypeError:        return value

But in our second overload statement how can we specify all values that don't support int?


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>