[Python-Dev] Re: RFC on Callable Type Syntax

2021年10月11日 15:23:30 -0700

On 2021年10月11日, Guido van Rossum wrote:
I always found the following more obvious:
def data_to_table(d: Iterable[Mapping[str, float]], *, sort: bool = False, 
reversed: bool = False) -> Table:
  ...
@dataclass
class Stream:
  converter: data_to_table | None
  def add_converter(self, converter: data_to_table) -> None:
    self.converter = converter
Another possibility would be that functions can't be used as their types directly, but need a casting operator like so:
```
  def add_converter(self, converter: typeof(data_to_table)) -> None:
    self.converter = converter
```
It's too bad `type` is already taken, but `typeof` is what TypeScript calls it: https://www.typescriptlang.org/docs/handbook/2/typeof-types.html -- perhaps there is a better distinguishing name, but I'll keep using `typeof` for the sake of this discussion. For static analysis, `typing.typeof` should only be used for variables that are assigned (certain) constant values and aren't re-assigned. Potentially this could be a much more general solution. Maybe interesting return values for `typeof` could be defined for `dict`s, `namedtuple`s, etc. And it could co-exist with the (P, Q) -> R proposal (which, as you probably know, is how TypeScript expresses function types).
One disadvantage of this is that now arguments HAVE TO be named which
raises questions:
- should they be considered at type checking time?
- how to express "I don't care"?
I think it'd be natural for types constructed via `typeof` to include the argument names; I'd intend for an object "just like" this one. But you could imagine another type operator that drops details like this, e.g. `typing.forget_argument_names(typing.typeof(data_table))`.
Erik
--
Erik Demaine | [email protected] | http://erikdemaine.org/ 
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/QVW4I2CJVEETXM7I437WYIXLCYSOP4B4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to