|
|
Subscribe / Log in / New account

Wrangling the typing PEPs

Wrangling the typing PEPs

Posted Dec 16, 2021 21:11 UTC (Thu) by pj (subscriber, #4506)
In reply to: Wrangling the typing PEPs by LtWorf
Parent article: Wrangling the typing PEPs

> I've seen libraries that enforce filenames to be "str" with a check, just to then call the python open function which would have gladly accepted pathlib.Path and bytes as well.

I feel like the correct way to do this would be to explicitly infer the type. Which meand we need some way to refer to 'the type that open()' takes as its first argument, but that's somewhat complicated to obtain:

```
import inspect

def mywrapper(filename: list(inspect.signature(open).parameters.values())[0].annotation):
...
```

...doesn't exactly flow from the fingertips. And I'm not sure if the if the value to the annotation is valid as an annotation itself. *sigh* IWBNI there was like an `infer(open, 1)`.


to post comments

Wrangling the typing PEPs

Posted Dec 17, 2021 6:39 UTC (Fri) by NYKevin (subscriber, #129325) [Link]

Runtime type checkers will probably be perfectly happy with that, but I am not aware of a single static type checker that actually evals the annotations in a full Python environment for you.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds