The idea looks a lot like TypeScript.

But, in Python we have different semantics. For example, this would mean that `some is list[str]` will be executed at runtime.
Which will most likely result in `False`. This will be a problem for runtime annotations and `get_type_hints`.
To fix this, we would need to change how `is` works: and it can open a black hole of all kinds of bugs.

Moreover, `is` is used for a completely different thing right now. It checks `id`s of objects.
I don't think that mixing these two completely different things in one operator will improve readability.

Quite a lot of trouble for very little gain :(



ср, 2 нояб. 2022 г. в 19:57, Job Veldhuis <jobbfveldhuis@gmail.com>:
I would like to propose a change to type guards, in order to make using them in code more readable. However, I am unsure how achievable this added functionality would be:

# Specification
I would like to have type guards as readable as human text. Currently we use type guards in the following manner:

```
def is_list_of_str(value: list[object]) -> TypeGuard[list[str]]:
  return all(isinstance(x, str) for x in value)
```

However, this whole TypeGuard with nested syntax isn't really that readable, is it? In TypeScript, I think this problem was solved in a more beautiful way. I would like to propose to use the same syntax as TypeScript for user defined type guards:

```
def is_list_of_str(value: list[object]) -> value is list[str]:
  return all(isinstance(x, str) for x in value)
```

Since this might not be achievable, because value is not defined at the moment of statically typing, we might want to do something along the lines of either:

```
def is_list_of_str(value: list[object]) -> "value" is list[str]:
  return all(isinstance(x, str) for x in value)

def is_list_of_str(value: list[object]) -> "value is list[str]":
  return all(isinstance(x, str) for x in value)
```

My preference would be the first one of the above examples, since this combines the readability with the current way used to type hint variables that may be defined later.

Please let me know whether or not you think this is worth pursuing, as well as your general thoughts on the subject.

- Job
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: n.a.sobolev@gmail.com