DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
http://docs.basedpyright.com/
Other
601 stars 12 forks source link

a system for types to define their own inference rules #436

Open DetachHead opened 1 week ago

DetachHead commented 1 week ago

currently the way inference works in all type checkers is a bunch of special-cased hueristics that are unclear to the user.

the type system should be able to declare inference rules, for example the type int could define its inferred type as int | float (as a way to fix #319)

maybe something like this:

# builtins.pyi
@infer(int | float)
class int:
    ...
a = 3 # inferred as int | float
a = a / 2# no error
KotlinIsland commented 1 week ago

tuple infer as literal?

a = 1,
a = 2,  # error
prashantraina commented 1 week ago

The first example already doesn't have any error, at least with the "basic" level of type checking.

a = 3 # inferred as Literal[3]
a = a / 2 # no error, the type of 'a' has changed to float
KotlinIsland commented 1 week ago

@prashantraina that's because currently it's special-cased to behave that way. This issue is looking to make that special cased behaviour standardised, denotable, and implementable to any types.