Skip to content

Sourcery refactored main branch#1

Open
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main
Open

Sourcery refactored main branch#1
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main

Conversation

@sourcery-ai
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot commented Dec 13, 2022

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from 2Sassy December 13, 2022 23:50

if base_query:
query = "%s&%s" % (base_query, query)
query = f"{base_query}&{query}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _build_api_url refactored with the following changes:

s = requests.Session()
proxies = _requests_proxies_arg(openai.proxy)
if proxies:
if proxies := _requests_proxies_arg(openai.proxy):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _make_session refactored with the following changes:

Comment on lines -98 to +99
str += "/%s" % (info["version"],)
str += f'/{info["version"]}'
if info["url"]:
str += " (%s)" % (info["url"],)
str += f' ({info["url"]})'
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function APIRequestor.format_app_info refactored with the following changes:

Comment on lines -252 to +253
user_agent = "OpenAI/v1 PythonBindings/%s" % (version.VERSION,)
user_agent = f"OpenAI/v1 PythonBindings/{version.VERSION}"
if openai.app_info:
user_agent += " " + self.format_app_info(openai.app_info)
user_agent += f" {self.format_app_info(openai.app_info)}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function APIRequestor.request_headers refactored with the following changes:

Comment on lines -325 to +328
abs_url = "%s%s" % (self.api_base, url)
abs_url = f"{self.api_base}{url}"
headers = self._validate_headers(supplied_headers)

data = None
if method == "get" or method == "delete":
if method in ["get", "delete"]:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function APIRequestor.request_raw refactored with the following changes:

Comment on lines -211 to +217
"label": labels if labels else empty_list,
"string": ["<br>".join(tr.wrap(string, width=30)) for string in strings]
"label": labels or empty_list,
"string": [
"<br>".join(tr.wrap(string, width=30)) for string in strings
]
if strings
else empty_list,
}
)
chart = px.scatter_3d(
return px.scatter_3d(
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function chart_from_components_3D refactored with the following changes:

Comment on lines -91 to -102
reduce_value = (
type(self), # callable
( # args
return (
type(self),
(
self.get("id", None),
self.api_key,
self.api_version,
self.api_type,
self.organization,
),
dict(self), # state
dict(self),
)
return reduce_value
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function OpenAIObject.__reduce__ refactored with the following changes:

This removes the following comments ( why? ):

# callable
# state
# args

Comment on lines -189 to +188
if stream:
assert not isinstance(response, OpenAIResponse) # must be an iterator
return (
util.convert_to_openai_object(
line,
api_key,
self.api_version,
self.organization,
plain_old_data=plain_old_data,
)
for line in response
)
else:
if not stream:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function OpenAIObject.request refactored with the following changes:

ident_parts.append(f'id={self.get("id")}')

return unicode_repr
return f'<{" ".join(ident_parts)} at {hex(id(self))}> JSON: {str(self)}'
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function OpenAIObject.__repr__ refactored with the following changes:

self._callback(self._progress)
except Exception as e: # catches exception from the callback
raise CancelledError("The upload was cancelled: {}".format(e))
raise CancelledError(f"The upload was cancelled: {e}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BufferReader.read refactored with the following changes:

for ac in additional_columns:
dups = [c for c in additional_columns if ac in c]
if len(dups) > 0:
if dups:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function additional_column_validator refactored with the following changes:

Comment on lines -208 to +209
if suffix_option == " ->":
if df.prompt.str.contains("\n").any():
continue
if suffix_option == " ->" and df.prompt.str.contains("\n").any():
continue
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function common_prompt_suffix_validator refactored with the following changes:

Comment on lines -349 to +348
if ft_type == "open-ended generation" or ft_type == "classification":
if ft_type in ["open-ended generation", "classification"]:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function common_completion_suffix_validator refactored with the following changes:

Comment on lines -583 to +586
if remediation.optional_msg is not None:
if accept_suggestion(input_text, auto_accept):
df = remediation.optional_fn(df)
optional_applied = True
if remediation.optional_msg is not None and accept_suggestion(
input_text, auto_accept
):
df = remediation.optional_fn(df)
optional_applied = True
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_optional_remediation refactored with the following changes:

pos_class = None
if n_classes == 2:
pos_class = df.completion.value_counts().index[0]
pos_class = df.completion.value_counts().index[0] if n_classes == 2 else None
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_classification_hyperparams refactored with the following changes:

return self.request(
"post",
self.instance_url() + "/generate",
f"{self.instance_url()}/generate",
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Engine.generate refactored with the following changes:

Comment on lines -34 to +36
return self.request("post", self.instance_url() + "/search", params)
return self.request("post", f"{self.instance_url()}/search", params)
else:
raise InvalidAPIType("Unsupported API type %s" % self.api_type)
raise InvalidAPIType(f"Unsupported API type {self.api_type}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Engine.search refactored with the following changes:

"Engine.embeddings is deprecated, use Embedding.create", DeprecationWarning
)
return self.request("post", self.instance_url() + "/embeddings", params)
return self.request("post", f"{self.instance_url()}/embeddings", params)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Engine.embeddings refactored with the following changes:

Comment on lines -42 to +46
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix, base, api_version)
url = f"/{cls.azure_api_prefix}{base}?api-version={api_version}"
elif typed_api_type == ApiType.OPEN_AI:
url = cls.class_url()
else:
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
raise error.InvalidAPIType(f"Unsupported API type {api_type}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function File.create refactored with the following changes:

Comment on lines -87 to +91
url = "/%s%s/%s/content?api-version=%s" % (
cls.azure_api_prefix,
base,
id,
api_version,
)
url = f"/{cls.azure_api_prefix}{base}/{id}/content?api-version={api_version}"
elif typed_api_type == ApiType.OPEN_AI:
url = f"{cls.class_url()}/{id}/content"
else:
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
raise error.InvalidAPIType(f"Unsupported API type {api_type}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function File.download refactored with the following changes:

@sourcery-ai
Copy link
Copy Markdown
Author

sourcery-ai bot commented Dec 13, 2022

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.12%.

Quality metrics Before After Change
Complexity 6.71 ⭐ 6.54 ⭐ -0.17 👍
Method Length 80.31 🙂 80.47 🙂 0.16 👎
Working memory 7.08 🙂 7.14 🙂 0.06 👎
Quality 61.18% 🙂 61.06% 🙂 -0.12% 👎
Other metrics Before After Change
Lines 4269 4186 -83
Changed files Quality Before Quality After Quality Change
openai/api_requestor.py 60.92% 🙂 60.84% 🙂 -0.08% 👎
openai/cli.py 63.77% 🙂 63.43% 🙂 -0.34% 👎
openai/embeddings_utils.py 58.20% 🙂 60.05% 🙂 1.85% 👍
openai/openai_object.py 77.57% ⭐ 76.90% ⭐ -0.67% 👎
openai/upload_progress.py 88.75% ⭐ 89.47% ⭐ 0.72% 👍
openai/validators.py 54.74% 🙂 55.15% 🙂 0.41% 👍
openai/wandb_logger.py 48.31% 😞 48.52% 😞 0.21% 👍
openai/api_resources/answer.py 96.74% ⭐ 96.74% ⭐ 0.00%
openai/api_resources/classification.py 96.74% ⭐ 96.74% ⭐ 0.00%
openai/api_resources/customer.py 90.25% ⭐ 90.25% ⭐ 0.00%
openai/api_resources/deployment.py 75.44% ⭐ 75.50% ⭐ 0.06% 👍
openai/api_resources/embedding.py 48.45% 😞 48.57% 😞 0.12% 👍
openai/api_resources/engine.py 77.90% ⭐ 77.70% ⭐ -0.20% 👎
openai/api_resources/file.py 46.37% 😞 45.35% 😞 -1.02% 👎
openai/api_resources/fine_tune.py 53.84% 🙂 51.86% 🙂 -1.98% 👎
openai/api_resources/image.py 67.04% 🙂 70.08% 🙂 3.04% 👍
openai/api_resources/moderation.py 79.80% ⭐ 79.80% ⭐ 0.00%
openai/api_resources/abstract/api_resource.py 71.89% 🙂 68.51% 🙂 -3.38% 👎
openai/api_resources/abstract/createable_api_resource.py 53.94% 🙂 52.36% 🙂 -1.58% 👎
openai/api_resources/abstract/deletable_api_resource.py 60.58% 🙂 58.09% 🙂 -2.49% 👎
openai/api_resources/abstract/engine_api_resource.py 45.77% 😞 44.12% 😞 -1.65% 👎
openai/api_resources/abstract/listable_api_resource.py 57.78% 🙂 56.44% 🙂 -1.34% 👎
openai/api_resources/abstract/nested_resource_class_methods.py 29.33% 😞 29.14% 😞 -0.19% 👎
openai/api_resources/abstract/updateable_api_resource.py 88.17% ⭐ 85.54% ⭐ -2.63% 👎
openai/tests/test_url_composition.py 94.05% ⭐ 94.05% ⭐ 0.00%

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
openai/wandb_logger.py WandbLogger._log_fine_tune 29 😞 312 ⛔ 16 ⛔ 20.46% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
openai/validators.py read_any_format 27 😞 314 ⛔ 17 ⛔ 20.47% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
openai/validators.py write_out_file 20 😞 366 ⛔ 16 ⛔ 24.68% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
openai/api_resources/abstract/engine_api_resource.py EngineAPIResource.create 15 🙂 297 ⛔ 21 ⛔ 26.39% 😞 Try splitting into smaller methods. Extract out complex expressions
openai/api_resources/abstract/nested_resource_class_methods.py nested_resource_class_methods 16 🙂 432 ⛔ 29.14% 😞 Try splitting into smaller methods

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Comment on lines -35 to +39
url = "/%s%s/%s/cancel?api-version=%s" % (
cls.azure_api_prefix,
base,
extn,
api_version,
)
url = f"/{cls.azure_api_prefix}{base}/{extn}/cancel?api-version={api_version}"
elif typed_api_type == ApiType.OPEN_AI:
url = "%s/%s/cancel" % (base, extn)
url = f"{base}/{extn}/cancel"
else:
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
raise error.InvalidAPIType(f"Unsupported API type {api_type}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FineTune.cancel refactored with the following changes:

Comment on lines -77 to +76
url = "/%s%s/%s/events?stream=true&api-version=%s" % (
cls.azure_api_prefix,
base,
extn,
api_version,
)
url = f"/{cls.azure_api_prefix}{base}/{extn}/events?stream=true&api-version={api_version}"
elif typed_api_type == ApiType.OPEN_AI:
url = "%s/%s/events?stream=true" % (base, extn)
url = f"{base}/{extn}/events?stream=true"
else:
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
raise error.InvalidAPIType(f"Unsupported API type {api_type}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FineTune.stream_events refactored with the following changes:

@classmethod
def _get_url(cls, action):
return cls.class_url() + f"/{action}"
return f"{cls.class_url()}/{action}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Image._get_url refactored with the following changes:

files: List[Any] = []
for key, value in params.items():
files.append((key, (None, value)))
files: List[Any] = [(key, (None, value)) for key, value in params.items()]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Image.create_variation refactored with the following changes:

Comment on lines -80 to +84
files: List[Any] = []
for key, value in params.items():
files.append((key, (None, value)))
files.append(("image", ("image", image, "application/octet-stream")))
files.append(("mask", ("mask", mask, "application/octet-stream")))

files: List[Any] = [(key, (None, value)) for key, value in params.items()]
files.extend(
(
("image", ("image", image, "application/octet-stream")),
("mask", ("mask", mask, "application/octet-stream")),
)
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Image.create_edit refactored with the following changes:

Comment on lines -173 to +177
url = "/%s/%s/%s/%s/%s?api-version=%s" % (
self.azure_api_prefix,
self.azure_deployments_prefix,
self.engine,
base,
extn,
api_version,
)
url = f"/{self.azure_api_prefix}/{self.azure_deployments_prefix}/{self.engine}/{base}/{extn}?api-version={api_version}"
params_connector = "&"

elif self.typed_api_type == ApiType.OPEN_AI:
base = self.class_url(self.engine, self.api_type, self.api_version)
url = "%s/%s" % (base, extn)
url = f"{base}/{extn}"

else:
raise error.InvalidAPIType("Unsupported API type %s" % self.api_type)
raise error.InvalidAPIType(f"Unsupported API type {self.api_type}")

timeout = self.get("timeout")
if timeout is not None:
timeout = quote_plus(str(timeout))
url += params_connector + "timeout={}".format(timeout)
url += f"{params_connector}timeout={timeout}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EngineAPIResource.instance_url refactored with the following changes:

Comment on lines -36 to +40
url = "/%s%s?api-version=%s" % (cls.azure_api_prefix, base, api_version)
url = f"/{cls.azure_api_prefix}{base}?api-version={api_version}"
elif typed_api_type == ApiType.OPEN_AI:
url = cls.class_url()
else:
raise error.InvalidAPIType("Unsupported API type %s" % api_type)
raise error.InvalidAPIType(f"Unsupported API type {api_type}")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ListableAPIResource.list refactored with the following changes:

):
if resource_plural is None:
resource_plural = "%ss" % resource
resource_plural = f"{resource}s"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function nested_resource_class_methods refactored with the following changes:

@classmethod
def modify(cls, sid, **params):
url = "%s/%s" % (cls.class_url(), quote_plus(sid))
url = f"{cls.class_url()}/{quote_plus(sid)}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UpdateableAPIResource.modify refactored with the following changes:

def test_engine_search_url_composition_default() -> None:
engine = Engine(id="test_id")
assert engine.api_type == None
assert engine.api_type is None
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_engine_search_url_composition_default refactored with the following changes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants