Skip to content

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

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
nadobando opened this issue Feb 3, 2021 · 3 comments
Closed

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

nadobando opened this issue Feb 3, 2021 · 3 comments
Assignees

Comments

@nadobando
Copy link
Contributor

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

@nadobando nadobando added RFC triage Pending triage from maintainers labels Feb 3, 2021
@to-mc to-mc added area/utilities and removed RFC labels Feb 3, 2021
@to-mc to-mc changed the title RFC: python38 does not need typing-extensions dependency feat: only install typing-extensions dependency when necessary Feb 3, 2021
@to-mc to-mc removed the triage Pending triage from maintainers label Feb 3, 2021
@to-mc to-mc self-assigned this Feb 3, 2021
@to-mc
Copy link
Contributor

to-mc commented Feb 3, 2021

Thanks @nadobando! I'll have a look at the PR shortly.

@heitorlessa
Copy link
Contributor

Now available in 1.10.2! Thank you very much for this

@hasnain2808
Copy link

@nadobando

typing-extensions module is a backport for typing hint for old versions of python <= 3.5

typing-extensions are also needed in say, version 3.8 to use type hints from later versions

Enable experimentation with new type system PEPs before they are accepted and added to the typing module.

https://pypi.org/project/typing-extensions/#description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

4 participants