8000 Infer types from issubclass() calls by pkch · Pull Request #3005 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Infer types from issubclass() calls #3005

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 16 commits into from
Apr 21, 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
Prev Previous commit
Next Next commit
More CR fixes
  • Loading branch information
pkch committed Mar 21, 2017
commit 81e19234191860985359b01040b3d536731bb76a
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,8 @@ def find_isinstance_check(node: Expression,
if isinstance(t, TypeType):
union_list.append(t.item)
else:
# this an error; should be caught earlier, so we should never be here
# this is an error that should be reported earlier
# if we reach here, we refuse to do any type inference
return {}, {}
Copy link
Member

Choose a reason for hiding this comment

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

If you are sure this is an unreachable branch, then you could put assert False here. If this could happen as a result of an error that is reported elsewhere, then adjust the comment correspondingly.

vartype = UnionType(union_list)
elif isinstance(vartype, TypeType):
Expand Down
4 changes: 2 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2696,9 +2696,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 # type: ignore
full_name = '%s.%s' % (file.fullname() if file is not None
else None, expr.name)
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 you don't need this formatting change, it only creates noise in the PR. Otherwise now it looks good to me

mod_name = " '%s'" % file.fullname() if file is not None else '' # type: ignore
mod_name = " '%s'" % file.fullname() if file is not None else ''
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
19 changes: 1 addition & 18 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1417,24 +1417,20 @@ def f(x: Union[int, A], a: Type[A]) -> None:
from typing import Type, ClassVar

class Goblin:
aggressive: bool = True
level: int
Copy link
Member

Choose a reason for hiding this comment

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

Do you need both attributes here? Some test cases could be simplified if you have only one instance variable.
Also you probably don't need to check both access and assignment to instance variables in all tests, just checking assignment is enough.


class GoblinAmbusher(Goblin):
job: ClassVar[str] = 'Ranger'


def test_issubclass(cls: Type[Goblin]) -> None:
if issubclass(cls, GoblinAmbusher):
cls.aggressive
cls.level
cls.job
ga = cls()
ga.level = 15
ga.job
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
else:
cls.aggressive
cls.level
cls.job # E: Type[Goblin] has no attribute "job"
g = cls()
Expand All @@ -1452,37 +1448,32 @@ class Mob:
pass

class Goblin(Mob):
aggressive: bool = True
level: int

class GoblinAmbusher(Goblin):
job: ClassVar[str] = 'Ranger'

def test_issubclass(cls: Type[Mob]) -> None:
if issubclass(cls, Goblin):
cls.aggressive
cls.level
cls.job # E: Type[Goblin] has no attribute "job"
g = cls()
g.level = 15
g.job # E: "Goblin" has no attribute "job"
if issubclass(cls, GoblinAmbusher):
cls.aggressive
cls.level
cls.job
g = cls()
g.level = 15
g.job
g.job = 'Warrior' # E: Cannot assign to class variable "job" via instance
else:
cls.aggressive # E: Type[Mob] has no attribute "aggressive"
cls.job # E: Type[Mob] has no attribute "job"
cls.level # E: Type[Mob] has no attribute "level"
m = cls()
m.level = 15 # E: "Mob" has no attribute "level"
m.job # E: "Mob" has no attribute "job"
if issubclass(cls, GoblinAmbusher):
cls.aggressive
cls.job
cls.level
ga = cls()
Expand All @@ -1491,7 +1482,6 @@ def test_issubclass(cls: Type[Mob]) -> None:
ga.job = 'Warrior' # E: Cannot assign to class variable "job" via instance

if issubclass(cls, GoblinAmbusher):
cls.aggressive
cls.level
cls.job
ga = cls()
Expand All @@ -1509,7 +1499,6 @@ class Mob:
pass

class Goblin(Mob):
aggressive: bool = True
level: int

class GoblinAmbusher(Goblin):
Expand All @@ -1518,17 +1507,14 @@ class GoblinAmbusher(Goblin):
class GoblinDigger(Goblin):
job: ClassVar[str] = 'Thief'


def test_issubclass(cls: Type[Mob]) -> None:
if issubclass(cls, (Goblin, GoblinAmbusher)):
cls.aggressive
cls.level
cls.job # E: Some element of union has no attribute "job"
g = cls()
g.level = 15
g.job # E: "Goblin" has no attribute "job"
if issubclass(cls, GoblinAmbusher):
cls.aggressive
cls.level
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
cls.job
Expand All @@ -1537,14 +1523,12 @@ def test_issubclass(cls: Type[Mob]) -> None:
ga.job
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
else:
cls.aggressive # E: Type[Mob] has no attribute "aggressive"
cls.job # E: Type[Mob] has no attribute "job"
cls.level # E: Type[Mob] has no attribute "level"
m = cls()
m.level = 15 # E: "Mob" has no attribute "level"
m.job # E: "Mob" has no attribute "job"
if issubclass(cls, GoblinAmbusher):
cls.aggressive
cls.job
cls.level
ga = cls()
Expand All @@ -1553,7 +1537,6 @@ def test_issubclass(cls: Type[Mob]) -> None:
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance

if issubclass(cls, (GoblinDigger, GoblinAmbusher)):
cls.aggressive
cls.level
cls.job
g = cls()
Expand All @@ -1574,7 +1557,7 @@ def f(cls: Type):
if issubclass(cls, MyList):
cls()[0]
else:
cls()[0] # E:
cls()[0]

if issubclass(cls, MyIntList):
cls()[0] + 1
Expand Down
2 changes: 1 addition & 1 deletion typeshed
Submodule typeshed updated 163 files
0