Skip to content

feat: only install typing-extensions dependency when necessary #280

Closed
@nadobando

Description

@nadobando

Key information

  • RFC PR: (leave this empty)
  • Related issue(s), if known:
  • Area: Parser
  • Meet tenets: Yes

Summary

typing-extensions module is a backport for typing hint for old versions of python <= 3.5
in this project many times typing-extensions is imported to use the Literal type hint which is already natively included in python >= 3.8

Motivation

There is no need for this package in Python >= 3.8 and we are currently enforced to install it

Proposal

add if try/except blocks based on the python version

# aws_lambda_powertools/utilities/parser/types.py
import sys

if sys.version_info[0] >= 3 and sys.version_info[1] >= 8:
    from typing import Literal  # noqa: F401
else:
    try:
        from typing_extensions import Literal  # noqa: F401
    except ImportError:
        raise Exception("please install typing-extensions or upgrade to Python >= 3.8")
# aws_lambda_powertools/utilities/parser/models/dynamodb.py
from datetime import date
from typing import Any, Dict, List, Optional

from pydantic import BaseModel

from ..types import Literal


class DynamoDBStreamChangedRecordModel(BaseModel):
    ApproximateCreationDateTime: Optional[date]
    Keys: Dict[str, Dict[str, Any]]
    NewImage: Optional[Dict[str, Any]]
    OldImage: Optional[Dict[str, Any]]
    SequenceNumber: str
    SizeBytes: int
    StreamViewType: Literal["NEW_AND_OLD_IMAGES", "KEYS_ONLY", "NEW_IMAGE", "OLD_IMAGE"]

this is not really a design, it's more a check to wether import typing-extensions or not based on the system python version

in order to use it right instead of importing typing_extensions we can include the dependency in the file types.py and then import that dependency from the types.py as showed in the code blocks

Drawbacks

since this is not a design it's more an improvement of a dependency import I don't see a drawback

Rationale and alternatives

  • What is the impact of not doing this?
    The impact of not doing this will be the used of a backport library which is not needed by the interpreter as it has the native implementation

Unresolved questions

no questions

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

Triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions