Skip to content

Add --disallow-subclassing-any flag #2100

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

Conversation

Michael0x2a
Copy link
Collaborator

This commit adds a new flag that disallows classes from having a base class of type Any.

This tends to happen when using --silent-imports or when using a class from a module or library that is not typechecked by mypy. Normally, mypy will silently accept subclassing Any, which can sometimes lead to discrepancies which further leads to subtle bugs.

Passing in this optional flag will raise an error instead.

@@ -770,6 +770,8 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
self.fail("Cannot subclass NewType", defn)
base_types.append(base)
elif isinstance(base, AnyType):
if self.options.disallow_subclassing_any:
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 it would be nice if this reported the name of the base class that's Any. You can probably extract that from base_expr at least in simple cases. Extra points if you can also report the module from it was imported (but I'm not sure if that info is easily available by the time we get here).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@gvanrossum -- whoops, I must have overlooked this comment! I've pushed a new diff that reports the name when possible. Though, as you suspected, finding the module the base class was imported from was somewhat tricky.

If you'd like, I can make the error message even more descriptive and leave a brief note explaining possible causes of this error and potential fixes.

This commit adds a new flag that disallows classes from having a base
class of type `Any`.

This tends to happen when using `--silent-imports` or when using a
class from a module or library that is not typechecked by mypy.
Normally, mypy will silently accept subclassing Any, which can sometimes
lead to discrepencies which further leads to subtle bugs.

Passing in this optional flag will raise an error instead.
This commit modifies the error message to try and report the specific
name of the invalid class whenever possible.
@Michael0x2a Michael0x2a force-pushed the add-flag-disallowing-subclassing-any branch from 546cd17 to bd6ae1f Compare September 8, 2016 17:30
@gvanrossum gvanrossum merged commit b8abcaf into python:master Sep 8, 2016
@Michael0x2a Michael0x2a deleted the add-flag-disallowing-subclassing-any branch September 9, 2016 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants