Skip to content

annotations subscripting containers cause TypeErrors #1607

@vchudnov-g

Description

@vchudnov-g

As per an internal doc, subscripting a container that does not inherit from typing.Generic can cause errors. In particular, a MutableSequence indexed by enums causes an error of the form
TypeError: Parameters to generic types must be types. This can be triggered, for example, by code of the form

        days_of_week: MutableSequence[dayofweek_pb2.DayOfWeek] = proto.RepeatedField(
            proto.ENUM,
            number=2,
            enum=dayofweek_pb2.DayOfWeek,
        )

which we are seeing in the internal part of our publishing pipeline.

Either of these fixes by itself works:

  1. Quoting the type: MutableSequence['dayofweek_pb2.DayOfWeek']
  2. Adding from __future__ import annotations at the top of the file. As per an internal reference, this seems to be related to PEP563, which has a status of Accepted as of 2017, so it's not clear why we would need a fix in the first place.

We should decide and implement the right fix.

Metadata

Metadata

Assignees

Labels

priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions