From 99ffbda279bf4c159511fb96b1d5bb688af25437 Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Thu, 21 Dec 2023 07:40:30 -0500 Subject: [PATCH 1/5] chore(internal): add bin script (#1001) --- bin/check-env-state.py | 40 ++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + requirements-dev.lock | 10 ++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 bin/check-env-state.py diff --git a/bin/check-env-state.py b/bin/check-env-state.py new file mode 100644 index 0000000000..e1b8b6cb39 --- /dev/null +++ b/bin/check-env-state.py @@ -0,0 +1,40 @@ +"""Script that exits 1 if the current environment is not +in sync with the `requirements-dev.lock` file. +""" + +from pathlib import Path + +import importlib_metadata + + +def should_run_sync() -> bool: + dev_lock = Path(__file__).parent.parent.joinpath("requirements-dev.lock") + + for line in dev_lock.read_text().splitlines(): + if not line or line.startswith("#") or line.startswith("-e"): + continue + + dep, lock_version = line.split("==") + + try: + version = importlib_metadata.version(dep) + + if lock_version != version: + print(f"mismatch for {dep} current={version} lock={lock_version}") + return True + except Exception: + print(f"could not import {dep}") + return True + + return False + + +def main() -> None: + if should_run_sync(): + exit(1) + else: + exit(0) + + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml index 91d3d79219..a9860b29ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,7 @@ dev-dependencies = [ "time-machine", "nox", "dirty-equals>=0.6.0", + "importlib-metadata>=6.7.0", "azure-identity >=1.14.1", "types-tqdm > 4" ] diff --git a/requirements-dev.lock b/requirements-dev.lock index 6df8805579..3e480ada33 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -11,7 +11,7 @@ annotated-types==0.6.0 anyio==4.1.0 argcomplete==3.1.2 attrs==23.1.0 -azure-core==1.29.5 +azure-core==1.29.6 azure-identity==1.15.0 black==23.3.0 certifi==2023.7.22 @@ -29,18 +29,19 @@ h11==0.14.0 httpcore==1.0.2 httpx==0.25.2 idna==3.4 +importlib-metadata==7.0.0 iniconfig==2.0.0 isort==5.10.1 msal==1.26.0 -msal-extensions==1.0.0 +msal-extensions==1.1.0 mypy==1.7.1 mypy-extensions==1.0.0 nodeenv==1.8.0 nox==2023.4.22 numpy==1.26.2 packaging==23.2 -pandas==2.1.3 -pandas-stubs==2.1.1.230928 +pandas==2.1.4 +pandas-stubs==2.1.4.231218 pathspec==0.11.2 platformdirs==3.11.0 pluggy==1.3.0 @@ -69,5 +70,6 @@ typing-extensions==4.8.0 tzdata==2023.3 urllib3==2.1.0 virtualenv==20.24.5 +zipp==3.17.0 # The following packages are considered to be unsafe in a requirements file: setuptools==68.2.2 From aa26106bb82ae812246b9b4a7ffc3dca2fdc9c8a Mon Sep 17 00:00:00 2001 From: JackYu Date: Thu, 21 Dec 2023 22:52:19 +0800 Subject: [PATCH 2/5] Fix missing comma in README code example (#1000) The previous version of the README contained a code example with a missing comma, which could lead to syntax errors. This commit corrects that issue by adding the missing comma. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f89d0bdb28..f644cdeefe 100644 --- a/README.md +++ b/README.md @@ -475,7 +475,7 @@ from openai import AzureOpenAI # gets the API Key from environment variable AZURE_OPENAI_API_KEY client = AzureOpenAI( # https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning - api_version="2023-07-01-preview" + api_version="2023-07-01-preview", # https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource azure_endpoint="https://example-endpoint.openai.azure.com", ) From 3c8dd718012702373326df9df55ceac4b8bfa169 Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:48:12 -0500 Subject: [PATCH 3/5] test: run the garbage collector less often (#1003) --- tests/test_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 0959185df2..ffa779fb38 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -208,8 +208,8 @@ def build_request(options: FinalRequestOptions) -> None: ITERATIONS = 10 for _ in range(ITERATIONS): build_request(options) - gc.collect() + gc.collect() snapshot_after = tracemalloc.take_snapshot() tracemalloc.stop() @@ -871,8 +871,8 @@ def build_request(options: FinalRequestOptions) -> None: ITERATIONS = 10 for _ in range(ITERATIONS): build_request(options) - gc.collect() + gc.collect() snapshot_after = tracemalloc.take_snapshot() tracemalloc.stop() From ceaf9a06fbd1a846756bb72cce50a69c8cc20bd3 Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Fri, 22 Dec 2023 05:45:53 -0500 Subject: [PATCH 4/5] chore(internal): use ruff instead of black for formatting (#1008) --- bin/{blacken-docs.py => ruffen-docs.py} | 130 +++++------------------- pyproject.toml | 12 ++- requirements-dev.lock | 5 +- src/openai/_models.py | 2 +- src/openai/_types.py | 16 +-- src/openai/_utils/_transform.py | 5 +- src/openai/_utils/_utils.py | 4 +- tests/test_transform.py | 4 +- 8 files changed, 50 insertions(+), 128 deletions(-) rename bin/{blacken-docs.py => ruffen-docs.py} (52%) diff --git a/bin/blacken-docs.py b/bin/ruffen-docs.py similarity index 52% rename from bin/blacken-docs.py rename to bin/ruffen-docs.py index 45d0ad1225..37b3d94f0f 100644 --- a/bin/blacken-docs.py +++ b/bin/ruffen-docs.py @@ -1,16 +1,14 @@ -# fork of https://github.com/asottile/blacken-docs implementing https://github.com/asottile/blacken-docs/issues/170 +# fork of https://github.com/asottile/blacken-docs adapted for ruff from __future__ import annotations import re +import sys import argparse import textwrap import contextlib +import subprocess from typing import Match, Optional, Sequence, Generator, NamedTuple, cast -import black -from black.mode import TargetVersion -from black.const import DEFAULT_LINE_LENGTH - MD_RE = re.compile( r"(?P^(?P *)```\s*python\n)" r"(?P.*?)" r"(?P^(?P=indent)```\s*$)", re.DOTALL | re.MULTILINE, @@ -19,55 +17,12 @@ r"(?P^(?P *)```\s*pycon\n)" r"(?P.*?)" r"(?P^(?P=indent)```.*$)", re.DOTALL | re.MULTILINE, ) -RST_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy")) -BLOCK_TYPES = "(code|code-block|sourcecode|ipython)" -DOCTEST_TYPES = "(testsetup|testcleanup|testcode)" -RST_RE = re.compile( - rf"(?P" - rf"^(?P *)\.\. (" - rf"jupyter-execute::|" - rf"{BLOCK_TYPES}:: (?P\w+)|" - rf"{DOCTEST_TYPES}::.*" - rf")\n" - rf"((?P=indent) +:.*\n)*" - rf"\n*" - rf")" - rf"(?P(^((?P=indent) +.*)?\n)+)", - re.MULTILINE, -) -RST_PYCON_RE = re.compile( - r"(?P" - r"(?P *)\.\. ((code|code-block):: pycon|doctest::.*)\n" - r"((?P=indent) +:.*\n)*" - r"\n*" - r")" - r"(?P(^((?P=indent) +.*)?(\n|$))+)", - re.MULTILINE, -) PYCON_PREFIX = ">>> " PYCON_CONTINUATION_PREFIX = "..." PYCON_CONTINUATION_RE = re.compile( rf"^{re.escape(PYCON_CONTINUATION_PREFIX)}( |$)", ) -LATEX_RE = re.compile( - r"(?P^(?P *)\\begin{minted}{python}\n)" - r"(?P.*?)" - r"(?P^(?P=indent)\\end{minted}\s*$)", - re.DOTALL | re.MULTILINE, -) -LATEX_PYCON_RE = re.compile( - r"(?P^(?P *)\\begin{minted}{pycon}\n)" r"(?P.*?)" r"(?P^(?P=indent)\\end{minted}\s*$)", - re.DOTALL | re.MULTILINE, -) -PYTHONTEX_LANG = r"(?Ppyblock|pycode|pyconsole|pyverbatim)" -PYTHONTEX_RE = re.compile( - rf"(?P^(?P *)\\begin{{{PYTHONTEX_LANG}}}\n)" - rf"(?P.*?)" - rf"(?P^(?P=indent)\\end{{(?P=lang)}}\s*$)", - re.DOTALL | re.MULTILINE, -) -INDENT_RE = re.compile("^ +(?=[^ ])", re.MULTILINE) -TRAILING_NL_RE = re.compile(r"\n+\Z", re.MULTILINE) +DEFAULT_LINE_LENGTH = 100 class CodeBlockError(NamedTuple): @@ -77,7 +32,6 @@ class CodeBlockError(NamedTuple): def format_str( src: str, - black_mode: black.FileMode, ) -> tuple[str, Sequence[CodeBlockError]]: errors: list[CodeBlockError] = [] @@ -91,24 +45,10 @@ def _collect_error(match: Match[str]) -> Generator[None, None, None]: def _md_match(match: Match[str]) -> str: code = textwrap.dedent(match["code"]) with _collect_error(match): - code = black.format_str(code, mode=black_mode) + code = format_code_block(code) code = textwrap.indent(code, match["indent"]) return f'{match["before"]}{code}{match["after"]}' - def _rst_match(match: Match[str]) -> str: - lang = match["lang"] - if lang is not None and lang not in RST_PY_LANGS: - return match[0] - min_indent = min(INDENT_RE.findall(match["code"])) - trailing_ws_match = TRAILING_NL_RE.search(match["code"]) - assert trailing_ws_match - trailing_ws = trailing_ws_match.group() - code = textwrap.dedent(match["code"]) - with _collect_error(match): - code = black.format_str(code, mode=black_mode) - code = textwrap.indent(code, min_indent) - return f'{match["before"]}{code.rstrip()}{trailing_ws}' - def _pycon_match(match: Match[str]) -> str: code = "" fragment = cast(Optional[str], None) @@ -119,7 +59,7 @@ def finish_fragment() -> None: if fragment is not None: with _collect_error(match): - fragment = black.format_str(fragment, mode=black_mode) + fragment = format_code_block(fragment) fragment_lines = fragment.splitlines() code += f"{PYCON_PREFIX}{fragment_lines[0]}\n" for line in fragment_lines[1:]: @@ -159,42 +99,33 @@ def _md_pycon_match(match: Match[str]) -> str: code = textwrap.indent(code, match["indent"]) return f'{match["before"]}{code}{match["after"]}' - def _rst_pycon_match(match: Match[str]) -> str: - code = _pycon_match(match) - min_indent = min(INDENT_RE.findall(match["code"])) - code = textwrap.indent(code, min_indent) - return f'{match["before"]}{code}' - - def _latex_match(match: Match[str]) -> str: - code = textwrap.dedent(match["code"]) - with _collect_error(match): - code = black.format_str(code, mode=black_mode) - code = textwrap.indent(code, match["indent"]) - return f'{match["before"]}{code}{match["after"]}' - - def _latex_pycon_match(match: Match[str]) -> str: - code = _pycon_match(match) - code = textwrap.indent(code, match["indent"]) - return f'{match["before"]}{code}{match["after"]}' - src = MD_RE.sub(_md_match, src) src = MD_PYCON_RE.sub(_md_pycon_match, src) - src = RST_RE.sub(_rst_match, src) - src = RST_PYCON_RE.sub(_rst_pycon_match, src) - src = LATEX_RE.sub(_latex_match, src) - src = LATEX_PYCON_RE.sub(_latex_pycon_match, src) - src = PYTHONTEX_RE.sub(_latex_match, src) return src, errors +def format_code_block(code: str) -> str: + return subprocess.check_output( + [ + sys.executable, + "-m", + "ruff", + "format", + "--stdin-filename=script.py", + f"--line-length={DEFAULT_LINE_LENGTH}", + ], + encoding="utf-8", + input=code, + ) + + def format_file( filename: str, - black_mode: black.FileMode, skip_errors: bool, ) -> int: with open(filename, encoding="UTF-8") as f: contents = f.read() - new_contents, errors = format_str(contents, black_mode) + new_contents, errors = format_str(contents) for error in errors: lineno = contents[: error.offset].count("\n") + 1 print(f"{filename}:{lineno}: code block parse error {error.exc}") @@ -217,15 +148,6 @@ def main(argv: Sequence[str] | None = None) -> int: type=int, default=DEFAULT_LINE_LENGTH, ) - parser.add_argument( - "-t", - "--target-version", - action="append", - type=lambda v: TargetVersion[v.upper()], - default=[], - help=f"choices: {[v.name.lower() for v in TargetVersion]}", - dest="target_versions", - ) parser.add_argument( "-S", "--skip-string-normalization", @@ -235,15 +157,9 @@ def main(argv: Sequence[str] | None = None) -> int: parser.add_argument("filenames", nargs="*") args = parser.parse_args(argv) - black_mode = black.FileMode( - target_versions=set(args.target_versions), - line_length=args.line_length, - string_normalization=not args.skip_string_normalization, - ) - retv = 0 for filename in args.filenames: - retv |= format_file(filename, black_mode, skip_errors=args.skip_errors) + retv |= format_file(filename, skip_errors=args.skip_errors) return retv diff --git a/pyproject.toml b/pyproject.toml index a9860b29ef..f1d1a66fef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,6 @@ managed = true dev-dependencies = [ "pyright", "mypy", - "black", "respx", "pytest", "pytest-asyncio", @@ -67,17 +66,18 @@ dev-dependencies = [ [tool.rye.scripts] format = { chain = [ - "format:black", - "format:docs", "format:ruff", + "format:docs", + "fix:ruff", "format:isort", ]} "format:black" = "black ." -"format:docs" = "python bin/blacken-docs.py README.md api.md" -"format:ruff" = "ruff --fix ." +"format:docs" = "python bin/ruffen-docs.py README.md api.md" +"format:ruff" = "ruff format" "format:isort" = "isort ." "check:ruff" = "ruff ." +"fix:ruff" = "ruff --fix ." typecheck = { chain = [ "typecheck:pyright", @@ -163,6 +163,8 @@ unfixable = [ ] ignore-init-module-imports = true +[tool.ruff.format] +docstring-code-format = true [tool.ruff.per-file-ignores] "bin/**.py" = ["T201", "T203"] diff --git a/requirements-dev.lock b/requirements-dev.lock index 3e480ada33..53763f2aa9 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -13,11 +13,9 @@ argcomplete==3.1.2 attrs==23.1.0 azure-core==1.29.6 azure-identity==1.15.0 -black==23.3.0 certifi==2023.7.22 cffi==1.16.0 charset-normalizer==3.3.2 -click==8.1.7 colorlog==6.7.0 cryptography==41.0.7 dirty-equals==0.6.0 @@ -42,7 +40,6 @@ numpy==1.26.2 packaging==23.2 pandas==2.1.4 pandas-stubs==2.1.4.231218 -pathspec==0.11.2 platformdirs==3.11.0 pluggy==1.3.0 portalocker==2.8.2 @@ -58,7 +55,7 @@ python-dateutil==2.8.2 pytz==2023.3.post1 requests==2.31.0 respx==0.20.2 -ruff==0.1.7 +ruff==0.1.9 six==1.16.0 sniffio==1.3.0 time-machine==2.9.0 diff --git a/src/openai/_models.py b/src/openai/_models.py index 5b8c96010f..330a2064d8 100644 --- a/src/openai/_models.py +++ b/src/openai/_models.py @@ -382,7 +382,7 @@ class RootModel(GenericModel, Generic[_T]): For example: ```py - validated = RootModel[int](__root__='5').__root__ + validated = RootModel[int](__root__="5").__root__ # validated: 5 ``` """ diff --git a/src/openai/_types.py b/src/openai/_types.py index a20a4b4c1b..fc26d5458a 100644 --- a/src/openai/_types.py +++ b/src/openai/_types.py @@ -278,11 +278,13 @@ class NotGiven: For example: ```py - def get(timeout: Union[int, NotGiven, None] = NotGiven()) -> Response: ... + def get(timeout: Union[int, NotGiven, None] = NotGiven()) -> Response: + ... + - get(timeout=1) # 1s timeout - get(timeout=None) # No timeout - get() # Default timeout behavior, which may not be statically known at the method definition. + get(timeout=1) # 1s timeout + get(timeout=None) # No timeout + get() # Default timeout behavior, which may not be statically known at the method definition. ``` """ @@ -304,14 +306,14 @@ class Omit: ```py # as the default `Content-Type` header is `application/json` that will be sent - client.post('/upload/files', files={'file': b'my raw file content'}) + client.post("/upload/files", files={"file": b"my raw file content"}) # you can't explicitly override the header as it has to be dynamically generated # to look something like: 'multipart/form-data; boundary=0d8382fcf5f8c3be01ca2e11002d2983' - client.post(..., headers={'Content-Type': 'multipart/form-data'}) + client.post(..., headers={"Content-Type": "multipart/form-data"}) # instead you can remove the default `application/json` header by passing Omit - client.post(..., headers={'Content-Type': Omit()}) + client.post(..., headers={"Content-Type": Omit()}) ``` """ diff --git a/src/openai/_utils/_transform.py b/src/openai/_utils/_transform.py index 9117559064..342b52416a 100644 --- a/src/openai/_utils/_transform.py +++ b/src/openai/_utils/_transform.py @@ -80,9 +80,10 @@ def transform( ```py class Params(TypedDict, total=False): - card_id: Required[Annotated[str, PropertyInfo(alias='cardID')]] + card_id: Required[Annotated[str, PropertyInfo(alias="cardID")]] - transformed = transform({'card_id': ''}, Params) + + transformed = transform({"card_id": ""}, Params) # {'cardID': ''} ``` diff --git a/src/openai/_utils/_utils.py b/src/openai/_utils/_utils.py index 993462a66b..cc624b0ce1 100644 --- a/src/openai/_utils/_utils.py +++ b/src/openai/_utils/_utils.py @@ -211,13 +211,15 @@ def required_args(*variants: Sequence[str]) -> Callable[[CallableT], CallableT]: def foo(*, a: str) -> str: ... + @overload def foo(*, b: bool) -> str: ... + # This enforces the same constraints that a static type checker would # i.e. that either a or b must be passed to the function - @required_args(['a'], ['b']) + @required_args(["a"], ["b"]) def foo(*, a: str | None = None, b: bool | None = None) -> str: ... ``` diff --git a/tests/test_transform.py b/tests/test_transform.py index 5e15385f4d..c4dffb3bb0 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -189,7 +189,9 @@ class DateDictWithRequiredAlias(TypedDict, total=False): def test_datetime_with_alias() -> None: assert transform({"required_prop": None}, DateDictWithRequiredAlias) == {"prop": None} # type: ignore[comparison-overlap] - assert transform({"required_prop": date.fromisoformat("2023-02-23")}, DateDictWithRequiredAlias) == {"prop": "2023-02-23"} # type: ignore[comparison-overlap] + assert transform({"required_prop": date.fromisoformat("2023-02-23")}, DateDictWithRequiredAlias) == { + "prop": "2023-02-23" + } # type: ignore[comparison-overlap] class MyModel(BaseModel): From 8213631ac079081f901fae85ec8948e95fcef56c Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Fri, 22 Dec 2023 05:46:35 -0500 Subject: [PATCH 5/5] release: 1.6.1 --- .release-please-manifest.json | 2 +- CHANGELOG.md | 9 +++++++++ pyproject.toml | 2 +- src/openai/_version.py | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 7deae33804..59565e8e31 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "1.6.0" + ".": "1.6.1" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 399e3aaebd..83bf20f775 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 1.6.1 (2023-12-22) + +Full Changelog: [v1.6.0...v1.6.1](https://github.com/openai/openai-python/compare/v1.6.0...v1.6.1) + +### Chores + +* **internal:** add bin script ([#1001](https://github.com/openai/openai-python/issues/1001)) ([99ffbda](https://github.com/openai/openai-python/commit/99ffbda279bf4c159511fb96b1d5bb688af25437)) +* **internal:** use ruff instead of black for formatting ([#1008](https://github.com/openai/openai-python/issues/1008)) ([ceaf9a0](https://github.com/openai/openai-python/commit/ceaf9a06fbd1a846756bb72cce50a69c8cc20bd3)) + ## 1.6.0 (2023-12-19) Full Changelog: [v1.5.0...v1.6.0](https://github.com/openai/openai-python/compare/v1.5.0...v1.6.0) diff --git a/pyproject.toml b/pyproject.toml index f1d1a66fef..f39cfb8f15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "openai" -version = "1.6.0" +version = "1.6.1" description = "The official Python library for the openai API" readme = "README.md" license = "Apache-2.0" diff --git a/src/openai/_version.py b/src/openai/_version.py index 9b01b6fcb1..9ab131d176 100644 --- a/src/openai/_version.py +++ b/src/openai/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. __title__ = "openai" -__version__ = "1.6.0" # x-release-please-version +__version__ = "1.6.1" # x-release-please-version