top | item 45419190

(no title)

Narushia | 5 months ago

By default, Mypy warns you if try to reassign a method of any object[1]. It will also warn you when you access non-existent attributes[2]. So if you have a variable typed as `object`, the only attributes you can manipulate without the type checker nagging are `__doc__`, `__dict__`, `__module__`, and `__annotations__`. Since there are very few reasons to ever reassign or manipulate these attributes on an instance, I think the `object` type gets us pretty darn close to an "unknown" type in practice.

There was a proposal[3] for an unknown type in the Python typing repository, but it was rejected on the grounds that `object` is close enough.

[1]: https://mypy.readthedocs.io/en/stable/error_code_list.html#c...

[2]: https://mypy.readthedocs.io/en/stable/error_code_list.html#c...

[3]: https://github.com/python/typing/issues/1835

discuss

order

letmeinhere|4 months ago

Thanks, I didn't have this context. I'll start using this!

lexicality|5 months ago

In my opinion the sheer volume of "close enough" choices is what ruins Python's type system.

It's "close enough" to a usable type system that it's worth using, but it's full of so many edge cases and so many situations where they decided that it would be easier if they forced programmers to try and make reality match the type system rather than the type system match reality.

No wonder a lot of people in the comments here say they don't use it...

Narushia|5 months ago

I think they can get away with the "close enough" solutions since Python's type annotations don't have any runtime contracts by default. Might be off-putting to people who are more familiar with statically typed languages (though not always, in my experience).