Skip to content

Classes should not inherit from Any #1446

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
emmatyping opened this issue Jun 30, 2017 · 6 comments · Fixed by #3591
Closed

Classes should not inherit from Any #1446

emmatyping opened this issue Jun 30, 2017 · 6 comments · Fixed by #3591
Labels
stubs: improvement Improve/refactor existing annotations, other stubs issues

Comments

@emmatyping
Copy link
Member

emmatyping commented Jun 30, 2017

Several classes in typeshed inherit from Any, and should not.

$ ag "class .*\(Any\)"
stdlib\3\multiprocessing\context.pyi
118:    class ForkProcess(Any):  # type: ignore
124:    class SpawnProcess(Any):  # type: ignore
130:    class ForkServerProcess(Any):  # type: ignore
148:    class SpawnProcess(Any):  # type: ignore

stdlib\3\types.pyi
81:class SimpleNamespace(Any): ...  # type: ignore

stdlib\3\unittest\mock.pyi
34:    class NonCallableMock(Any):

tests\pytype_blacklist.txt
11:# Because of 'class ForkProcess(Any):  # type: ignore'

third_party\3\jwt\algorithms.pyi
3:class Algorithm(Any): ...  # type: ignore

As can be seen, all but one of these are type-ignore'd and thus not picked up by mypy.

The multiprocessing classes have # TODO: type should be BaseProcess once a stub in multiprocessing.process exists, so that should be okay to change, as that module is there.

The types.pyi SimpleNameSpace needed my __setattr__ implementation.

Apparently the same for NonCallableMock

JWT stubs just seem very incomplete.

@JelleZijlstra
Copy link
Member

Feel free to send some PRs. :)

toejough added a commit to toejough/typeshed that referenced this issue Jul 18, 2017
**Problem**:
Any time I run `mypy`, I get an error like `<site-packages>/lib/mypy/typeshed/stdlib/3/unittest/mock.pyi:34: error: Class cannot subclass 'Any' (has type 'Any')`

**Analysis**:
python#1446 complains about the inheritance from `Any`, among other things, but what's relevant to this PR is where the use of `# type: ignore` is followed everywhere else this inheritance happens.

I'm not sure if it's the "right" solution, but adding `# type: ignore` to `mock.pyi`'s use of `Any` resolves the error message I keep seeing.  It also passes all of the [tests requested](https://github.com/python/typeshed/blob/master/README.md#running-the-tests).

**Resolution**:
I added the same `# type: ignore` comment to the inheritance from `Any` in `mock.pyi` that is used in other places that that inheritance is done.

**Documentation**:
No docs modified/added for this change.

**Testing**:
Ran the [tests](https://github.com/python/typeshed/blob/master/README.md#running-the-tests) locally on osx 10.11.6, and they all passed.

**Impact**:
Patch - ignoring an illegal type inheritance that appears to work but is flagged by mypy (and is ignored in other areas of typeshed code).
@srittau
Copy link
Collaborator

srittau commented Sep 11, 2018

Updated list:

stdlib/3/multiprocessing/context.pyi
134:    class ForkProcess(Any):  # type: ignore
140:    class SpawnProcess(Any):  # type: ignore
146:    class ForkServerProcess(Any):  # type: ignore
164:    class SpawnProcess(Any):  # type: ignore

third_party/3/jwt/algorithms.pyi
3:class Algorithm(Any): ...  # type: ignore

third_party/2and3/google/protobuf/internal/containers.pyi
51:class Mapping(Any): ...

Some are fixed, the protobuf one is new, but is marked explicitly as "not yet typed".

srittau added a commit to srittau/typeshed that referenced this issue Oct 21, 2018
JelleZijlstra pushed a commit that referenced this issue Oct 23, 2018
@srittau srittau added the stubs: improvement Improve/refactor existing annotations, other stubs issues label Oct 28, 2018
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this issue Jan 23, 2019
@hauntsaninja
Copy link
Collaborator

Looks like it's only the protobuf containers one left. Is it worth adding a test to prevent the addition of Any others?

@srittau
Copy link
Collaborator

srittau commented Jan 8, 2020

I think adding a test to flake8-pyi would make sense.

@JelleZijlstra
Copy link
Member

It would be better to turn on mypy's --disallow-subclassing-any flag (maybe with a type ignore for the protobuf ones).

@srittau
Copy link
Collaborator

srittau commented Jan 8, 2020

Good point.

srittau added a commit to srittau/typeshed that referenced this issue Jan 8, 2020
JelleZijlstra pushed a commit that referenced this issue Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: improvement Improve/refactor existing annotations, other stubs issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants