Skip to content

Commit

Permalink
Use typing_extension.Literal in codegen for Python 3.7
Browse files Browse the repository at this point in the history
Also don't install typing-extension on Python 3.8 and above
  • Loading branch information
fantix committed Oct 20, 2022
1 parent 23dd42e commit 6d0d6ab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
33 changes: 17 additions & 16 deletions edgedb/codegen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,26 +434,27 @@ def _generate_code(
print(file=buf)
self._imports.add("typing")
if SYS_VERSION_INFO >= (3, 8):
for el_name, el_code in link_props:
print(f"{INDENT}@typing.overload", file=buf)
print(
f'{INDENT}def __getitem__'
f'(self, key: typing.Literal["@{el_name}"]) '
f'-> {el_code}:',
file=buf,
)
print(f"{INDENT}{INDENT}...", file=buf)
print(file=buf)
typing_literal = "typing.Literal"
else:
self._imports.add("typing_extensions")
typing_literal = "typing_extensions.Literal"
for el_name, el_code in link_props:
print(f"{INDENT}@typing.overload", file=buf)
print(
f'{INDENT}def __getitem__'
f'(self, key: {typing_literal}["@{el_name}"]) '
f'-> {el_code}:',
file=buf,
)
print(f"{INDENT}{INDENT}...", file=buf)
print(file=buf)
print(
f"{INDENT}def __getitem__(self, key: str) -> typing.Any:",
file=buf,
)
if SYS_VERSION_INFO >= (3, 8):
print(
f"{INDENT}{INDENT}raise NotImplementedError", file=buf
)
else:
print(f"{INDENT}{INDENT}...", file=buf)
print(
f"{INDENT}{INDENT}raise NotImplementedError", file=buf
)

self._defs[rv] = buf.getvalue().strip()

Expand Down
5 changes: 3 additions & 2 deletions edgedb/credentials.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import pathlib
import sys
import typing
import json

try:
if sys.version_info >= (3, 8):
from typing import TypedDict
except ImportError:
else:
from typing_extensions import TypedDict

from . import platform
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def finalize_options(self):
cmdclass={'build_ext': build_ext},
test_suite='tests.suite',
install_requires=[
'typing-extensions>=3.10.0',
'typing-extensions>=3.10.0; python_version < "3.8.0"',
'certifi>=2021.5.30; platform_system == "Windows"',
],
extras_require=EXTRA_DEPENDENCIES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dataclasses
import datetime
import edgedb
import typing
import typing_extensions
import uuid


Expand All @@ -31,9 +32,17 @@ class LinkPropResultFriendsItem(NoPydanticValidation):
id: uuid.UUID
name: str

def __getitem__(self, key: str) -> typing.Any:
@typing.overload
def __getitem__(self, key: typing_extensions.Literal["@created_at"]) -> typing.Optional[datetime.datetime]:
...

@typing.overload
def __getitem__(self, key: typing_extensions.Literal["@strength"]) -> typing.Optional[float]:
...

def __getitem__(self, key: str) -> typing.Any:
raise NotImplementedError


async def link_prop(
client: edgedb.AsyncIOClient,
Expand Down

0 comments on commit 6d0d6ab

Please sign in to comment.