microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 769 forks source link

Valid code marked as unreachable #2276

Closed semberal closed 2 years ago

semberal commented 2 years ago

Hello,

valid code using the foundationdb module is being marked as unreachable by Pylance:

import fdb
fdb.api_version(630)
db = fdb.open()
print("This line is reported as unreachable")

fdb.open() by default raises an error until fdb.api_version(version) is called, which modifies the behavior of some global methods like open(). Pylance seems not to understand this and marks all code below open() as unreachable.

Versions

Pylance extension: v2022.1.3 Python extension: v2021.12.1559732655 Python: 3.9.9 foundationdb pip package: 6.3.23

erictraut commented 2 years ago

This package provides no type annotations, so pylance is attempting to infer type information from the sources through static analysis. A static analyzer won't be able to predict dynamic behaviors like the one you described above. The library appears to be dynamically manipulating the global variables in its module namespace.

If this library is important to you, I recommend contacting the maintainers and encouraging them to provide type annotations. This will improve the developer experience for users of the library. Library maintainers can refer to this guidance.

To fix this particular issue, a minimal type annotation on open would suffice.

def open(*args, **kwargs) -> typing.Any: ...