Commit Graph

2 Commits

Author SHA1 Message Date
Stephen Macke
c821b83ab3 [typing] make mypy-protobuf output compatible with pyre for caffe2 type stubs (#55294)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55294

Some static checkers like pyre have difficulties with types like `builtings.type`, so we strip the `builtins` prefix from autogened proto type stubs.

Test Plan: Let CI run.

Reviewed By: d4l3k

Differential Revision: D27477699

fbshipit-source-id: 45e19835974200a030817d37aec785e3ecb23e8b
2021-04-05 03:23:31 -07:00
Stephen Macke
a8885ee7e6 [BE][typing] add caffe2/torch proto stubs (1 of 2) (#52341)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52341

Add type stubs for caffe2 protos and scripts for generating them.

It's worth calling special attention to the following. In order to make `DeviceType`s like `CPU`, `CUDA`, etc. directly accessible from the `caffe2_pb2` module, they are currently freedom-patched into it in `caffe2/python/__init__.py`. This is not ideal: it would be better if these were autogenerated when the protobuf definitions were created by using `allow_alias = true` in the `DeviceTypeProto` definition in `caffe2.proto`.

However, it is impossible to do this currently without significant effort. The issue is that the generated proto constants would conflict with various constants defined in the C++ caffe2 codebase in `caffe2_pb.h`. We cannot simply remove these constants and replace them with the caffe2 DeviceTypeProto constants, because a huge portion of code expects `at::DeviceType` constants defined in `core/DeviceType.h` (apparently duplicated to avoid having to figure out how to autogenerate the protobuf definitions using cmake for ATen).

Instead, we make a best-effort to add additional definitions in `caffe2_pb2.py` by looking for any freedom-patched constants in `caffe2/python/__init__.py` and making sure they have corresponding stubs in the pyi (see `gen_proto_typestubs_helper.py`).

Test Plan: Make sure CI is green; we're just adding stubs.

Reviewed By: d4l3k

Differential Revision: D26331875

fbshipit-source-id: 2eea147e5bf393542f558ff8cf6385c47624b770
2021-02-17 10:30:11 -08:00