-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
No errors on typed function internals if function definition itself is untyped; I'd argue that's probably incorrect behavior, but at the very least it's *super* confusing and *very* hard to diagnose when it happens #10895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
With
|
I have now verified this happens with Python versions 3.6.14, 3.7.11, 3.8.2, 3.8.11, and 3.9.6. |
I think this is because of https://mypy.readthedocs.io/en/stable/common_issues.html#no-errors-reported-for-obviously-wrong-code. Does running mypy with |
% rm -fr .mypy_cache ; mypy --config=/dev/null --check-untyped-defs test_case3.py
/dev/null: No [mypy] section in config file
test_case3.py:5: error: No overload variant of "__getitem__" of "list" matches argument type "str"
test_case3.py:5: note: Possible overload variants:
test_case3.py:5: note: def __getitem__(self, SupportsIndex) -> int
test_case3.py:5: note: def __getitem__(self, slice) -> List[int]
test_case3.py:8: error: No overload variant of "__getitem__" of "list" matches argument type "str"
test_case3.py:8: note: Possible overload variants:
test_case3.py:8: note: def __getitem__(self, SupportsIndex) -> int
test_case3.py:8: note: def __getitem__(self, slice) -> List[int]
Found 2 errors in 1 file (checked 1 source file) I still think this is a bug, for two reasons. First, the fact that it works at the module level and not the function level is just weird. Second, I swear this used to work with classes that defined their own # test_case4.py
from typing import List
def func():
a: List[int] = [0, 1, 2]
key: str = "asdf"
a[key] # <- This doesn't generate an error
func()
b: List[int] = [0, 1, 2]
key: str = "asdf"
b[key] # <- But this does?! Runtime:
|
Ugh, talk about a super sharp edge. This works as expected (since I typed the enclosing function): # test_case5.py
from typing import List
def func() -> None:
a: List[int] = [0, 1, 2]
key: str = "asdf"
a[key] # <- *Now* this generates an error
func()
b: List[int] = [0, 1, 2]
key: str = "asdf"
b[key] # <- So does this |
As does this: # test_case6.py
from typing import List, Union
class GetItemTest:
def __getitem__(self, key: Union[int, slice]) -> Union[int, List[int]]:
return list(range(10))[key]
def test_a() -> None:
a = GetItemTest()
a["asdf"] # <- errors |
This is yet another example that supports my thinking that |
Maybe as a work-around, but functions seem like pretty arbitrary boundaries to try and walk this particular line, especially where bodies contain explicit type annotations. I can't see how ignoring those can be considered correct under any setting. In other words, yeah, totally make the sane thing the default thing, but I'm pretty sure this is still a bug. |
It's documented that mypy doesn't type check unannotated functions (https://mypy.readthedocs.io/en/stable/common_issues.html#no-errors-reported-for-obviously-wrong-code). I don't disagree with you that this is unintuitive, but the other issue is a sufficient place to discuss that. |
Yes, this appears to be a dupe of #3948. But I don't think the documentation is even accurate. Consider: # test_case9.py
from typing import Callable
a: Callable[[], int]
def _untyped_init_a():
global a
a = lambda: [0, 1, 2]["key"] # errors
def _untyped_init_b():
global b
b = lambda: [0, 1, 2]["key"] # doesn't error According to the docs, |
Issue description
This doesn't generate an error:
This does:
That maybe technically "documented", but that's really esoteric and completely unintuitive. I've spent hours chasing this down.
Archived original flailing to arrive at core problem
UPDATE 2: Okay, this just keeps getting weirder and weirder. Consider:
Runtime:
UPDATE 1: You don't need the
@overload
s. It still doesn't work without them. Here's the simplified case:Original case (same effect):
Runtime:
I can't prove it, but I feel like this used to work. It seems pretty basic.
The text was updated successfully, but these errors were encountered: