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

release: 1.6.1 #1002

Merged
merged 5 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "1.6.0"
".": "1.6.1"
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down
40 changes: 40 additions & 0 deletions bin/check-env-state.py
Original file line number Diff line number Diff line change
@@ -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()
130 changes: 23 additions & 107 deletions bin/blacken-docs.py → bin/ruffen-docs.py
Original file line number Diff line number Diff line change
@@ -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<before>^(?P<indent> *)```\s*python\n)" r"(?P<code>.*?)" r"(?P<after>^(?P=indent)```\s*$)",
re.DOTALL | re.MULTILINE,
Expand All @@ -19,55 +17,12 @@
r"(?P<before>^(?P<indent> *)```\s*pycon\n)" r"(?P<code>.*?)" r"(?P<after>^(?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<before>"
rf"^(?P<indent> *)\.\. ("
rf"jupyter-execute::|"
rf"{BLOCK_TYPES}:: (?P<lang>\w+)|"
rf"{DOCTEST_TYPES}::.*"
rf")\n"
rf"((?P=indent) +:.*\n)*"
rf"\n*"
rf")"
rf"(?P<code>(^((?P=indent) +.*)?\n)+)",
re.MULTILINE,
)
RST_PYCON_RE = re.compile(
r"(?P<before>"
r"(?P<indent> *)\.\. ((code|code-block):: pycon|doctest::.*)\n"
r"((?P=indent) +:.*\n)*"
r"\n*"
r")"
r"(?P<code>(^((?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<before>^(?P<indent> *)\\begin{minted}{python}\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
re.DOTALL | re.MULTILINE,
)
LATEX_PYCON_RE = re.compile(
r"(?P<before>^(?P<indent> *)\\begin{minted}{pycon}\n)" r"(?P<code>.*?)" r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
re.DOTALL | re.MULTILINE,
)
PYTHONTEX_LANG = r"(?P<lang>pyblock|pycode|pyconsole|pyverbatim)"
PYTHONTEX_RE = re.compile(
rf"(?P<before>^(?P<indent> *)\\begin{{{PYTHONTEX_LANG}}}\n)"
rf"(?P<code>.*?)"
rf"(?P<after>^(?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):
Expand All @@ -77,7 +32,6 @@ class CodeBlockError(NamedTuple):

def format_str(
src: str,
black_mode: black.FileMode,
) -> tuple[str, Sequence[CodeBlockError]]:
errors: list[CodeBlockError] = []

Expand All @@ -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)
Expand All @@ -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:]:
Expand Down Expand Up @@ -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}")
Expand All @@ -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",
Expand All @@ -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


Expand Down
15 changes: 9 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -51,7 +51,6 @@ managed = true
dev-dependencies = [
"pyright",
"mypy",
"black",
"respx",
"pytest",
"pytest-asyncio",
Expand All @@ -60,23 +59,25 @@ dev-dependencies = [
"time-machine",
"nox",
"dirty-equals>=0.6.0",
"importlib-metadata>=6.7.0",
"azure-identity >=1.14.1",
"types-tqdm > 4"
]

[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",
Expand Down Expand Up @@ -162,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"]
Expand Down
15 changes: 7 additions & 8 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ 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
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
Expand All @@ -29,19 +27,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
pathspec==0.11.2
pandas==2.1.4
pandas-stubs==2.1.4.231218
platformdirs==3.11.0
pluggy==1.3.0
portalocker==2.8.2
Expand All @@ -57,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
Expand All @@ -69,5 +67,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
Loading