Skip to content

pylint should warn about type(x) == Y and type(x) is Y #299

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
pylint-bot opened this issue Aug 5, 2014 · 3 comments
Closed

pylint should warn about type(x) == Y and type(x) is Y #299

pylint-bot opened this issue Aug 5, 2014 · 3 comments
Labels
Enhancement ✨ Improvement to a component

Comments

@pylint-bot
Copy link

Originally reported by: Chris Rebert (BitBucket: cvrebert, GitHub: @cvrebert?)


People who are new to Python often forget that the isinstance() built-in function exists, and instead end up writing their typechecks non-idiomatically, using type() and an equality comparison. Here's an example:

#!python

def dispatch(x):
    if type(x) == int:
        print "int"
    elif type(x) is dict:
        print "dict"

pylint doesn't currently emit any relevant warnings for this code. I propose it should emit warnings suggesting that the code be rewritten to use isinstance() instead.

Corrected example:

#!python

def typecheck(x):
    if isinstance(x, int):
        print "int"
    elif isinstance(x, dict):
        print "dict"

(Yes, I know that these aren't strictly equivalent due to subclasses and metaclasses, but those cases are pretty uncommon.)


@pylint-bot
Copy link
Author

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore):


Merged in cvrebert/pylint/impl-299 (pull request #227)

Fix #299: Warn about type(x) is/== Y

@pylint-bot
Copy link
Author

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore):


Fix #299: Warn about type(x) is/== Y

@pylint-bot
Copy link
Author

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore):


Merged in cvrebert/pylint/impl-299 (pull request #227)

Fix #299: Warn about type(x) is/== Y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

No branches or pull requests

1 participant