Skip to content

self.__class__.__name__ has type Any in class derived from Any #4058

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
gvanrossum opened this issue Oct 4, 2017 · 9 comments · Fixed by #4038
Closed

self.__class__.__name__ has type Any in class derived from Any #4058

gvanrossum opened this issue Oct 4, 2017 · 9 comments · Fixed by #4038

Comments

@gvanrossum
Copy link
Member

Here's something that came up in python/typeshed#1549 (comment).

from typing import Any, Type, TypeVar
T = TypeVar('T')
class C(Any):
    def bar(self: T) -> Type[T]: ...
    def foo(self) -> None:
        reveal_type(self.bar())  # Type[C], as expected
        reveal_type(self.bar().__name__)  # Any, but expected str

@ilevkivskyi I think you muttered something about analyze_member_access for TypeType still being broken but I'm not sure I understand.

@ilevkivskyi
Copy link
Member

This will be fixed by #4038

@gvanrossum
Copy link
Member Author

Ah, thanks. I'm waiting for Jukka to review that, it's a bit above my review capacity ATM.

@ilevkivskyi
Copy link
Member

I will add this example to the tests in the meantime.

@gvanrossum
Copy link
Member Author

FWIW, the issue in our internal codebase that prompted this report is not resolved by #4038. But that issue is more complex than my example here, and I haven't boiled it down yet. To avoid referencing the other (closed, typeshed) issue, here is the problem again.

Some file gives this error:

[...]/task.py:221: error: Cannot determine type of '__name__'

The source code at that line looks like this:

from threading import local
...
class TrackedTask(local):
    def __init__(self, ...):
        # type: ...
        if SupportedTrackedTaskWorkers.is_supported(self.__class__.__name__, self.task_name): ...

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Oct 4, 2017

Hm, strange, I can't reproduce this. I replaced SupportedTrackedTaskWorkers.is_supported with len and removed the second argument and everything works, also reveal_type(self.__class__.__name__) shows builtns.str. I tried other combinations but still can't get this error.

@gvanrossum
Copy link
Member Author

Me neither. :-( There must be something else in the file that affects this, but I don't yet know what it is. There must be something really strange going on, since if I add reveal_type(self.__class__.__name__) I get both these:

devtools/changes/changes/queue/task.py:221: error: Revealed type is 'builtins.str'
devtools/changes/changes/queue/task.py:221: error: Cannot determine type of '__name__'

@JelleZijlstra
Copy link
Member

Regardless of the possible mypy bug, could we fix this in typeshed by making threading.local not inherit from Any and instead override __setattr__ and __getattr__?

@gvanrossum
Copy link
Member Author

gvanrossum commented Oct 4, 2017 via email

JukkaL pushed a commit that referenced this issue Oct 11, 2017
This fixes the second traceback reported in #3852. This also improves member 
lookup on `Type[...]` by moving it to a later stage (`checkmember.py` instead 
of some manipulations in `TypeType` constructor that are dangerous during 
de-serialization).

Also fixes the first case in #4058.
@gvanrossum
Copy link
Member Author

gvanrossum commented Oct 11, 2017 via email

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

Successfully merging a pull request may close this issue.

3 participants