11 ms·
The right type is ‘object’, not ‘Any’. ‘object’ is the base class of all types: you can put anything into an ‘object’, and you can only do very generic operati
by anderskaseorg 7y ago
The right type is ‘object’, not ‘Any’.
‘object’ is the base class of all types: you can put anything into an ‘object’, and you can only do very generic operations like str() on what you get out of an ‘object’ without further checks like isinstance().
‘Any’ is an unsound escape hatch that disables type checking: you can put anything into an ‘Any’, and you can do anything with what you get out of an ‘Any’, and the type checker will make no effort to stop you from doing something wrong.
https://docs.python.org/3/library/typing.html#the-any-type https://docs.python.org/3/library/typing.html#the-any-type
https://mypy.readthedocs.io/en/latest/dynamic_typing.html#any-vs-object https://mypy.readthedocs.io/en/latest/dynamic_typing.html#an...
- takeda 7y agoActually the proper way to handle is to use protocols. They provide already SupportsBytes type, and you can similarly create custom SupportsStr, but IMO it is silly to do that. This approach increases chance of bugs, and it's just easy to use str type and for developer to wrap such value in str(). I believe that's why SupportsStr was not introduced.