Skip to content

Commit 6967517

Browse files
authored
Import cleanup (#259)
* Use typing_extensions.TypedDict, remove unused imports * Enforce typing_extensions.TypedDict Fixes: #209 * Add lifetime instructions
1 parent 6b281da commit 6967517

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

google/generativeai/types/answer_types.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,10 @@
1414
# limitations under the License.
1515
from __future__ import annotations
1616

17-
import abc
18-
import dataclasses
19-
from typing import Any, Dict, List, TypedDict, Optional, Union
17+
from typing import Union
2018

2119
import google.ai.generativelanguage as glm
2220

23-
from google.generativeai import string_utils
24-
from google.generativeai.types import safety_types
25-
from google.generativeai.types import citation_types
26-
from google.generativeai.types import content_types
27-
2821
__all__ = ["Answer"]
2922

3023
FinishReason = glm.Candidate.FinishReason

google/generativeai/types/content_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import inspect
66
import mimetypes
77
import typing
8-
from typing import Any, Callable, TypedDict, Union
8+
from typing import Any, Callable, Union
9+
from typing_extensions import TypedDict
910

1011
import pydantic
1112

google/generativeai/types/file_types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from __future__ import annotations
1616

1717
import datetime
18-
from typing import TypedDict
1918

2019
from google.generativeai.client import get_default_file_client
2120

google/generativeai/types/generation_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import dataclasses
2222
import itertools
2323
import textwrap
24-
from typing import TypedDict, Union
24+
from typing import Union
25+
from typing_extensions import TypedDict
2526

2627
import google.protobuf.json_format
2728
import google.api_core.exceptions

tests/test_typing_extensions.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import pathlib
17+
import re
18+
19+
from absl.testing import absltest
20+
21+
TYPING_RE = re.compile(r"\btyping\b.*TypedDict")
22+
23+
24+
class TypingExtensionsTests(absltest.TestCase):
25+
"""Pydantic users need the improved version of TypedDict, from typing_extensions.
26+
27+
This is only required for python versions <3.12. Once 3.12 is the lowest supported version we can drop this.
28+
29+
ref: https://docs.pydantic.dev/2.3/usage/types/dicts_mapping/
30+
31+
> the typing-extensions package is required for Python <3.12
32+
"""
33+
34+
def test_no_typing_typed_dict(self):
35+
root = pathlib.Path(__file__).parent.parent
36+
for fpath in root.rglob("*.py"):
37+
source = fpath.read_text()
38+
if match := TYPING_RE.search(source):
39+
raise ValueError(
40+
f"Please import `TypedDict` from `typing_extensions` not `typing`, in:", fpath
41+
)
42+
43+
44+
if __name__ == "__main__":
45+
absltest.main()

0 commit comments

Comments
 (0)