KotlinIsland / basedmypy

Based Python static type checker with baseline, sane default settings and based typing features
https://kotlinisland.github.io/basedmypy/
Other
146 stars 4 forks source link

type-time static `StaticPath` types #436

Open KotlinIsland opened 1 year ago

KotlinIsland commented 1 year ago

Checking if files exist statically would be useful

a: StaticPath["foo.txt"] = Path("foo.txt")  # error: file doesn't exists

Expanding on this we could even statically get the content of the files:

a: StaticPath['foo.txt']
reveal_type(a.read_text())  # "Hello, World!"
BjoernPetersen commented 1 year ago

How and why would you do this? Python programs aren't always (probably not even most of the time) developed in the same working directory or even on the same system on which they're supposed to be executed. Files – especially their content – seem inherently non-static to me.

DetachHead commented 1 year ago

files are certainly not always static, but sometimes they are, so it would be useful to have them statically checked in those cases.

it would be optional, perhaps specifying a generic would make it statically checked:

a = Path["foo.txt"]() # generic could be reified using basedtyping.ReifiedGeneric

but normal paths would not be checked:

a = Path("foo.txt")

typescript for example allows you to import json files and have them checked at compiletime:

// config.json
{
    "foo": 1
}
import config from './config.json'
config.bar // error
KotlinIsland commented 1 year ago

What about StaticPath?