Skip to content
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

Create Direction type to allow more type-safe usage of direction #582

Closed
johnthagen opened this issue Oct 19, 2021 · 1 comment
Closed
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@johnthagen
Copy link
Contributor

johnthagen commented Oct 19, 2021

When writing an extension, I found an area that could be improved to help down stream developers that use type checking or IDEs (such as PyCharm) that support type annotations:

def map_serializer(self, auto_schema: 'AutoSchema', direction):

def is_patched_serializer(serializer, direction):

Usage of direction throughout the codebase could be augmented with the use of a dedicated Direction Enum or Literal type:

@unique
class Direction(Enum):
    Request = "request"
    Response = "response"

Or

from typing import Literal

Direction = Literal["request", "response"]

The Literal type was added in Python 3.8, so for supporting older versions, they typing-extensions package backports these in a PyPI package.

Resulting code base would look like:

def map_serializer(self, auto_schema: 'AutoSchema', direction: Direction): 
@tfranzel
Copy link
Owner

Hey @johnthagen,

very good point. Unfortunately using an Enum could break existing users. I therefore opted for the Literal solution. We recently added typing-extensions as explicit dep anyway.

@tfranzel tfranzel added enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants