Skip to content
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

Rename variable to avoid mypy type error #23669

Merged
merged 4 commits into from
May 19, 2020
Merged

Conversation

stephenmcgruer
Copy link
Contributor

@stephenmcgruer stephenmcgruer commented May 18, 2020

Reusing variables can cause a mypy type error (python/mypy#1174).
This previously went unnoticed because the previous and current use of key was str, but in
#23644 the previous use was retyped to Text
and as such caused a unicode-vs-str type error in mypy.

@stephenmcgruer stephenmcgruer changed the title Testing MYPY complaints for sourcefile.py [WIP] Testing MYPY complaints for sourcefile.py May 18, 2020
@wpt-pr-bot wpt-pr-bot temporarily deployed to wpt-preview-23669 May 18, 2020 16:41 Inactive
@wpt-pr-bot wpt-pr-bot temporarily deployed to wpt-preview-23669 May 18, 2020 17:40 Inactive
@stephenmcgruer stephenmcgruer changed the title [WIP] Testing MYPY complaints for sourcefile.py Teach mypy that __cached_properties__ is a Set[str] May 18, 2020
@stephenmcgruer stephenmcgruer marked this pull request as ready for review May 18, 2020 18:37
@stephenmcgruer
Copy link
Contributor Author

I'd like either @Hexcles or @gsnedders to look at this, as their mypy knowledge far exceeds mine.

@wpt-pr-bot wpt-pr-bot temporarily deployed to wpt-preview-23669 May 18, 2020 18:41 Inactive
@stephenmcgruer
Copy link
Contributor Author

Note that this didn't start complaining until #23644, so it may be that there is some problem here and this is the wrong fix

With #23644 reverted, I get:

cached_properties: Revealed type is 'Any'
key: Revealed type is 'builtins.str'
self.__dict__: Revealed type is 'builtins.dict[builtins.str, Any]'

With that change, key changes to builtins.unicode, so maybe worth digging into.

@stephenmcgruer
Copy link
Contributor Author

stephenmcgruer commented May 18, 2020

Ok yes, reverting #23644 and doing some bisecting I narrowed this to the change in type of def script_metadata. I've no idea why yet, but reverting the entire change and then applying just that line causes key to flip from str to unicode.

@gsnedders
Copy link
Member

gsnedders commented May 18, 2020

With #23644 reverted, I get:

cached_properties: Revealed type is 'Any'
key: Revealed type is 'builtins.str'
self.__dict__: Revealed type is 'builtins.dict[builtins.str, Any]'

What are the revealed types with it, but without the str call?

I'm slightly surprised to see key isn't also Any there, but given it's used to lookup in the Dict[str, Any] I guess it isn't that surprising.

@stephenmcgruer
Copy link
Contributor Author

What are the revealed types with it, but without the str call?

Not sure if I am misunderstanding, but that PR added the str() call - so those are the types without it.

@gsnedders
Copy link
Member

Not sure if I am misunderstanding, but that PR added the str() call - so those are the types without it.

Pretty sure I'm misunderstanding now, given:

Ok yes, reverting #23644 and doing some bisecting I narrowed this to the change in type of def script_metadata. I've no idea why yet, but reverting the entire change and then applying just that line causes key to flip from str to unicode.

So if #23644 caused it to flip to unicode, revealing the types must've changed without the str call to give unicode somewhere, no?

@stephenmcgruer
Copy link
Contributor Author

Soooo I renamed key to j and:

tools/manifest/sourcefile.py:997: note: Revealed type is 'Any'

I think mypy is getting confused with the earlier declarations of key in the same code?

@stephenmcgruer
Copy link
Contributor Author

jgraham points me to: python/mypy#1174

@stephenmcgruer
Copy link
Contributor Author

So options are:

i. Do the type annotation like this PR does - it works today to nudge mypy into realizing the new type of 'key', but not guaranteed to work always I guess?
ii. Change the variable name to something else.
iii. Leave it with the cast.

I'm open to opinions. I probably lean to (ii).

@jgraham
Copy link
Contributor

jgraham commented May 18, 2020

I think I prefer i to iii in that order. But I'm happy with i or ii and less with iii.

@Hexcles
Copy link
Member

Hexcles commented May 18, 2020

Great investigation, folks!

I think the root cause here is indeed the redefinition of key and I prefer renaming the variable (to prop perhaps).

@Hexcles
Copy link
Member

Hexcles commented May 18, 2020

Question: was this just a warning instead of an error from mypy? (I'm wondering how #23644 passed.)

@stephenmcgruer
Copy link
Contributor Author

Question: was this just a warning instead of an error from mypy? (I'm wondering how #23644 passed.)

Because that PR wrapped key in str(key) explicitly.

@Hexcles
Copy link
Member

Hexcles commented May 18, 2020 via email

@stephenmcgruer
Copy link
Contributor Author

And you noticed when reviewing the code?

@foolip did: #23644 (comment)

@stephenmcgruer stephenmcgruer changed the title Teach mypy that __cached_properties__ is a Set[str] Rename variable to avoid mypy type error May 18, 2020
@stephenmcgruer stephenmcgruer merged commit 7b9a66f into master May 19, 2020
@stephenmcgruer stephenmcgruer deleted the smcgruer/testing branch May 19, 2020 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants