Skip to content

v3.5.0 - TypeError: 'ABCMeta' object is not subscriptable on Python 3.8 — Awaitable[T] in utils.py incompatible with <3.9 #143

Description

@angandotra-coursera

Bug report

Summary

aws-embedded-metrics fails to import on Python 3.8. The library crashes at module load time with TypeError: 'ABCMeta' object is not subscriptable, preventing any application that imports it from starting.

Environment

  • Python version: 3.8.x
  • aws-embedded-metrics version: 3.5.0 (also reproduced on current master)
  • Platform: SageMaker SKLearn container (miniconda3 Python 3.8)

Steps to reproduce

pip install aws-embedded-metrics
python3.8 -c "from aws_embedded_metrics import metric_scope"

Full traceback

File "aws_embedded_metrics/utils.py", line 33, in <module>
    async def _await(awaitable: Awaitable[T]) -> T:
TypeError: 'ABCMeta' object is not subscriptable

Root cause

In aws_embedded_metrics/utils.py, Awaitable is imported from collections.abc:

from collections.abc import Awaitable

It is then used as a generic type annotation at module scope:

async def _await(awaitable: Awaitable[T]) -> T:

Generic subscripting of collections.abc classes (e.g. Awaitable[T]) was only made legal at runtime in Python 3.9 via PEP 585. On Python 3.8, this expression is evaluated eagerly when the module is first imported, raising TypeError because ABCMeta does not implement __class_getitem__.

Proposed fix

change the import to use typing.Awaitable instead of collections.abc.Awaitable:

# Before
from collections.abc import Awaitable

# After
from typing import Awaitable

typing.Awaitable has supported [T] subscripting since Python 3.5.3 and is safe on all supported Python versions.

Additional notes

  • The entire import chain (metric_scopemetrics_logger_factorymetrics_contextutils) fails because the crash occurs at module scope on every import.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions