PEP 724: Stricter Type Guards

This seems like a reasonable idea, and suggests IsInstance[T] for the type name. Even if there are subtle differences, maybe documenting them under a note explaining why the semantics can’t be exactly the same would be enough.

For me, the two names would then make sense

  • TypeGuard - asserts that a value is of a given type, but says nothing when it can’t make that assertion.
  • IsInstance - confirms if a value is or is not of a given type.

“Guard” has the sense of a one-sided check, where “is” feels two-sided to me.

On a more general note, it would be nice if cases like isinstance, where type checkers special-case particular builtins and stdlib names (I believe dataclass is another one) we’re listed somewhere and documented. At the moment they tend to come up in discussions as unverified presumptions, which is not ideal.

6 Likes