Skip to content

fixes for existing diagnostic rules

reportRedeclaration and reportDuplicateImport

pyright does not report redeclarations if the redeclaration has the same type:

foo: int = 1
foo: int = 2  # no error

nor does it care if you have a duplicated import in multiple different import statements, or in aliases:

from foo import bar
from bar import bar  # no error
from baz import foo as baz, bar as baz  # no error

basedpyright solves both of these problems by always reporting an error on a redeclaration or an import with the same name as an existing import.

reportUnreachable

reportUnreachable was the first new diagnostic rule that was added to basedpyright, however this rule was recently added to pyright too, but their version is far less safe. specifically, it doesn't report an error on sys.version_info or sys.platform checks, which are by far the most common cases where pyright considers code to be unreachable.

the reason we added reportUnreachable to basedpyright was not just to identify code that will never be reached, but mainly to identify code that will not be type checked.

Example

assuming that you're running python 3.13 or above during development:

if sys.version_info < (3, 13):
    1 + "" # no error

normally 1 + "" would be reported as a type error but pyright doesn't complain here, because unreachable code doesn't get type checked at all! this is bad of course, because chances are if your code contains an if statement like this, you're expecting it to be run on multiple different python versions.

reportInvalidTypeVarUse

pyright incorrectly reports an error when a function contains a type var only in the return position, in cases where there's no valid alternative:

Example

# error: TypeVar "T" appears only once in generic function signature
#   Use "object" instead
def empty_list[T]() -> list[T]:
    return []

# using `object` as suggested will cause an error here:
foo: list[int] = empty_list()

basedpyright will not report this error if the type var is used only in the return position, as long as it's possible for the function to safely return that type at runtime.

however the error will still be reported if the function's return type is just the type var itself, for example:

Example

# error: TypeVar "T" appears only once in generic function signature
#   Use "Never" instead
def fn[T]() -> T:
    ...

foo: int = fn()

here, pyright will incorrectly suggest returning object instead, of Never. there's no way for this function to safely return a T, but changing its return type to object will cause an error on the usage. basedpyright will correctly suggest changing the return type to Never instead.