8000 Fix inference logic for isinstance by pkch · Pull Request #2997 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix inference logic for isinstance #2997

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

Merged
merged 8 commits into from
Mar 19, 2017
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
Next Next commit
Make no inference when variables present in second argument to isinst…
…ance
  • Loading branch information
pkch committed Mar 19, 2017
commit a29069e1c487468d7b50a538890e11a6aeb40893
11 changes: 8 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2739,9 +2739,14 @@ def get_isinstance_type(expr: Expression, type_map: Dict[Expression, Type]) -> T

types.append(type)

Copy link
Member

Choose a reason for hiding this comment

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

I think there are too many blank lines here. IMO the only blank line should be outside the for-loop, before the assert on line 2720 below.

if len(types) == 0:
return None
elif len(types) == 1:
elif isinstance(type, TypeType):
types.append(type.item)

else: # we didn't see an actual type, but rather a variable whose value is unknown to us
return None

assert len(types) != 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we should return None if the length if 0? For example, what happens if we have isinstance(x, ())?

if len(types) == 1:
return types[0]
else:
return UnionType(types)
Expand Down
7 changes: 4 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2681,7 +2681,7 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
# bar in its namespace. This must be done for all types of bar.
file = base.node
assert isinstance(file, (MypyFile, type(None)))
n = file.names.get(expr.name, None) if file is not None else None
n = file.names.get(expr.name, None) if file is not None else None # type: ignore
if n:
n = self.normalize_type_alias(n, expr)
if not n:
Expand All @@ -2697,8 +2697,9 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
# one type checker run. If we reported errors here,
# the build would terminate after semantic analysis
# and we wouldn't be able to report any type errors.
full_name = '%s.%s' % (file.fullname() if file is not None else None, expr.name)
mod_name = " '%s'" % file.fullname() if file is not None else ''
full_name = '%s.%s' % (file.fullname() if file is not None # type: ignore
else None, expr.name)
mod_name = " '%s'" % file.fullname() if file is not None else '' # type: ignore
if full_name in obsolete_name_mapping:
self.fail("Module%s has no attribute %r (it's now called %r)" % (
mod_name, expr.name, obsolete_name_mapping[full_name]), expr)
Expand Down
0