Skip to content

mypy ignores annotated types in various circumstances #4636

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

Closed
rotemdan opened this issue Feb 26, 2018 · 3 comments
Closed

mypy ignores annotated types in various circumstances #4636

rotemdan opened this issue Feb 26, 2018 · 3 comments

Comments

@rotemdan
Copy link

rotemdan commented Feb 26, 2018

Edit: some of the error messages here later turned out not to be related to mypy, see this comment.

Sorry if duplicate! (I wasn't sure what keywords to use to search this)
(using mypy 0.560)

The following doesn't error:

from typing import List

def main():
	test: List[List[int]] = []
	test[0]['abcd'] = 'hi' # <-- no error here?

but this does:

from typing import List

def main():
	test: List[List[int]] = [[]]
	test[0]['abcd'] = 'hi' # <- does error here

And it gets even more bizarre:

from typing import List

def f(a: List[List[int]]) -> List[List[int]]:
	# a.append([1,2,3]) # <-- uncommenting this line seems to have no effect as well
	return a

def main():
	test = f([])
	test[0]['abcd'] = 'hi' # <- doesn't error?

However:

from typing import List

def f(a: List[List[int]]) -> List[List[int]]:
	return a

def main():
	test = f([[]])
	test[0]['abcd'] = 'hi' # <- does error

And if the argument to f has a type unknown to mypy, it doesn't error as well (this is the form which I originally discovered this behavior):

from typing import List

def f(a: List[List[int]]) -> List[List[int]]:
	return a

def main():
	import someModule
	unknown = someModule.getSomething()
	test = f(unknown)
	test[0]['abcd'] = 'hi' # <- doesn't error
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 26, 2018

main is missing a type annotation so it doesn't get type checked. You should add a return type (def main() -> None: ...) and mypy will produce more useful results.

@rotemdan
Copy link
Author

rotemdan commented Feb 26, 2018

@JukkaL

Thanks, adding the return type appears to help superficially (I've spent hours at trying to figure this out),
but it still seems quite unexpected that some inferences work but some don't? I'm not sure how this relates to the return type of main? (why can't it be inferred as None by default?)

I find it difficult to understand the logic of this?

Basically if the scoping function has no return type annotation, things 'sort' of work, but strangely/incorrectly?

(In any case, If the return type annotation is so critically important, it would be helpful if mypy would notify about it)

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 26, 2018

There is an open issues about this (#3948) with some discussion. The documentation could be a bit clearer about this. Mypy doesn't type check functions without annotations to avoid generating spurious errors for partially annotated legacy code.

@JukkaL JukkaL closed this as completed Feb 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants