You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Please just change something. Weird combination of dead code and inferred None #10634
I think warn_unreachable should be included in --strict, and I don't see this as a bad thing in a project that uses --strict. A --strict project doesn't need checks like that because of the type checker. If a function like this is really needed, then it would be better to annotate val as object or Any.
I think the problem here is that mypy is inferring a partial None for foo, and then inconsistently using that fact when type checking bar.
I suppose mypy could try rechecking bar repeatedly until all partial types stopped changing using something similar to what we do for type checking loops, but I think the better fix is just disallowing partial types that can't be inferred completely using only the scope they were defined in by enabling --local-partial-types.
With --local-partial-types enabled, the output I'm getting is:
foo=None# E: Need type annotation for "foo"defbar() ->str: # E: Missing return statementglobalfooiffooisnotNone:
print("Hello")
else:
foo="bar"return"lol"
which is a lot closer to what I'd expect.
Merging #10169 would make --local-partial-types the default, which should fix this issue.
I agree that --local-partial-types is the right fix. Making it the default is blocked for now since the switch can be quite disruptive for users. Improving the error messages could help. For example, we could suggest --no-local-partial-types or similar in a note. We could also perhaps allow disabling most of the new errors selectively, while still otherwise supporting local partial types.
I disagree that making --local-partial-types the default is the right fix. That would degrade mypy's ability to infer types for the user, so it would be a diminishment in mypy's usefulness. Instead, the partial type inference should be fixed, which would improve mypy's ability to infer types for the user.
Activity
Akuli commentedon Jun 13, 2021
I set
warn_unreachable = Trueand discovered other similar bugs in existing code. Can we enablewarn_unreachableby default?scaramallion commentedon Jun 15, 2021
Having
warn_unreachableas the default would flag errors in code that performs checking along the lines of:Akuli commentedon Jun 15, 2021
I think
warn_unreachableshould be included in--strict, and I don't see this as a bad thing in a project that uses--strict. A--strictproject doesn't need checks like that because of the type checker. If a function like this is really needed, then it would be better to annotatevalasobjectorAny.sobolevn commentedon Sep 30, 2021
related #11223
pranavrajpal commentedon May 13, 2022
I think the problem here is that mypy is inferring a partial None for
foo, and then inconsistently using that fact when type checkingbar.I suppose mypy could try rechecking
barrepeatedly until all partial types stopped changing using something similar to what we do for type checking loops, but I think the better fix is just disallowing partial types that can't be inferred completely using only the scope they were defined in by enabling--local-partial-types.With
--local-partial-typesenabled, the output I'm getting is:which is a lot closer to what I'd expect.
Merging #10169 would make
--local-partial-typesthe default, which should fix this issue.JukkaL commentedon May 13, 2022
I agree that
--local-partial-typesis the right fix. Making it the default is blocked for now since the switch can be quite disruptive for users. Improving the error messages could help. For example, we could suggest--no-local-partial-typesor similar in a note. We could also perhaps allow disabling most of the new errors selectively, while still otherwise supporting local partial types.wyattscarpenter commentedon Jan 25, 2026
I disagree that making
--local-partial-typesthe default is the right fix. That would degrade mypy's ability to infer types for the user, so it would be a diminishment in mypy's usefulness. Instead, the partial type inference should be fixed, which would improve mypy's ability to infer types for the user.