Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,9 @@ def process_newtype_declaration(self, s: AssignmentStmt) -> None:
self.fail(message.format(old_type), s)
return

if 'unimported' in self.options.disallow_any and has_any_from_unimported_type(old_type):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing this finally made me realize why I'm not fond of the way you're organizing your flags. It's easy to imagine having a typo somewhere, e.g. if 'unimprted in self.options.disalloy_any and the mistake would only be detected by careful unit testing. OTOH if the test in the code was if self.options.disallow_any_unimprted the static checks would flag it right away. Note that this is independent from the way the command-line UI looks -- you could still have --disallow-any=unimported but it would have to set a Boolean attribute disallow_any_unimported in the Options object.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I think it's a very good point.
I will implement this in a different PR.

self.msg.unimported_type_becomes_any("Argument 2 to NewType(...)", old_type, s)

# If so, add it to the symbol table.
node = self.lookup(name, s)
if node is None:
Expand Down
9 changes: 5 additions & 4 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,13 @@ main:5: error: Constraint 2 becomes List[Any] due to an unfollowed import

[case testDisallowImplicitAnyNewType]
# flags: --ignore-missing-imports --disallow-any=unimported
from typing import NewType
from typing import NewType, List
from missing import Unchecked

Baz = NewType('Baz', Unchecked)
[out]
main:5: error: Argument 2 to NewType(...) must be subclassable (got Any)
# this case is handled elsewhere: Baz = NewType('Baz', Unchecked)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still include the test here. Tests shouldn't follow the implementation too closely -- they should tests features.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yeah, that makes sense. I was just worried that if someone changes the wording, they have to change the tests in several places vs in one spot. But I guess it's not too much work.

Baz = NewType('Baz', List[Unchecked]) # E: Argument 2 to NewType(...) becomes List[Any] due to an unfollowed import

[builtins fixtures/list.pyi]

[case testDisallowImplicitAnyCallableAndTuple]
# flags: --ignore-missing-imports --disallow-any=unimported
Expand Down