JelleZijlstra / autotyping

Automatically add simple type annotations to your code
215 stars 18 forks source link

autotyping does not seem to find if/else branches that do not explicitly return #40

Closed twoertwein closed 1 year ago

twoertwein commented 1 year ago

autotyping annotates the following function as returning bool but it should be bool | None (example from pandas/util/_test_decorators.py)

def _skip_if_no_mpl():
    mod = safe_import("matplotlib")
    if mod:
        mod.use("Agg")
    else:
        return True

def _skip_if_not_us_locale():
    lang, _ = locale.getlocale()
    if lang != "en_US":
        return True
jakkdl commented 1 year ago

I think this is bad style, and is disallowed by e.g. pylint's inconsistent-return-statements and pyanalyze's missing_return. It would also be quite complicated to try to infer all possible program flows through a function.

JelleZijlstra commented 1 year ago

I am going to document this as a known limitation. I don't want to add the logic to implement this properly to autotyping. In the general case (e.g., an exhaustive match statement over an enum) it requires the capabilities of a full type checker.