Skip to content

Commit

Permalink
run black on whole repo (#2572)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed May 6, 2024
1 parent 25bf66d commit adfeb94
Show file tree
Hide file tree
Showing 407 changed files with 8,309 additions and 8,562 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/require_black_all-2024-4-6-14-29-30.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: internal
packages:
- "@autorest/python"
- "@azure-tools/typespec-python"
---

run black on whole repo
2 changes: 1 addition & 1 deletion eng/pipelines/ci-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ steps:
workingDirectory: $(Build.SourcesDirectory)/autorest.python/packages/${{parameters.folderName}}
condition: and(succeeded(), ${{ parameters.pythonCodeChecks }})

- script: black ${{parameters.pythonFolderName}}
- script: black $(Build.SourcesDirectory)/autorest.python
displayName: Black
workingDirectory: $(Build.SourcesDirectory)/autorest.python/packages/${{parameters.folderName}}
condition: and(succeeded(), ${{ parameters.pythonCodeChecks }})
Expand Down
15 changes: 8 additions & 7 deletions eng/scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
pipeline_build = is_pipeline_build()
root_dir = Path.resolve(Path(__file__) / "../../..")


def main(package_name: str, version_suffix: str, output_dir: str, package_versions: dict):
if package_name == "autorest.python":
versionVariableName = "generatorVersion"
Expand Down Expand Up @@ -46,9 +47,7 @@ def main(package_name: str, version_suffix: str, output_dir: str, package_versio

if pipeline_build:
# Update version variable
print(
f"##vso[task.setvariable variable={versionVariableName};isOutput=true]{newVersion}"
)
print(f"##vso[task.setvariable variable={versionVariableName};isOutput=true]{newVersion}")

# Build project
call("pnpm run build")
Expand Down Expand Up @@ -133,15 +132,17 @@ def main(package_name: str, version_suffix: str, output_dir: str, package_versio
if "autorest.python" in package_versions:
version = package_versions["autorest.python"]
if args.publish_internal:
overrides["@autorest/python"] = f'{feedUrl}/@autorest/python/-/python-{version}.tgz'
overrides["@autorest/python"] = f"{feedUrl}/@autorest/python/-/python-{version}.tgz"

if "typespec-python" in package_versions:
version = package_versions["typespec-python"]
if args.publish_internal:
overrides["@azure-tools/typespec-python"] = f'{feedUrl}/@azure-tools/typespec-python/-/typespec-python-{version}.tgz'

overrides["@azure-tools/typespec-python"] = (
f"{feedUrl}/@azure-tools/typespec-python/-/typespec-python-{version}.tgz"
)

with open(output_dir / "overrides.json", "w") as fp:
json.dump(overrides, fp, indent=2)
json.dump(overrides, fp, indent=2)

with open(output_dir / "package-versions.json", "w") as fp:
json.dump(package_versions, fp, indent=2)
8 changes: 5 additions & 3 deletions eng/scripts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def str2bool(v):
else:
raise argparse.ArgumentTypeError("Boolean value expected.")


def is_pipeline_build():
return not not os.environ.get("TF_BUILD", "")

def call(cmd:str, cwd=root_dir, suppress_failures=False, capture_output=False):

def call(cmd: str, cwd=root_dir, suppress_failures=False, capture_output=False):
print("\n============================================================")
print(f"From {cwd}\n> {cmd}")
print("============================================================", flush=True)
Expand All @@ -45,13 +47,13 @@ def call(cmd:str, cwd=root_dir, suppress_failures=False, capture_output=False):

returncode = process.returncode

if(capture_output):
if capture_output:
print(process.stdout.rstrip(), flush=True)

elapsed_time = time.time() - start_time

command_failed = returncode != 0 and not suppress_failures

print("------------------------------------------------------------")
if command_failed:
print(f"##[error]Command failed: {cmd}")
Expand Down
10 changes: 2 additions & 8 deletions eng/scripts/run_apiview.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ def _single_dir_apiview(mod):
]
)
except CalledProcessError as e:
if (
loop >= 2
): # retry for maximum 3 times because sometimes the apistubgen has transient failure.
logging.error(
"{} exited with apiview generation error {}".format(
mod.stem, e.returncode
)
)
if loop >= 2: # retry for maximum 3 times because sometimes the apistubgen has transient failure.
logging.error("{} exited with apiview generation error {}".format(mod.stem, e.returncode))
return False
else:
loop += 1
Expand Down
8 changes: 2 additions & 6 deletions eng/scripts/run_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@


def _single_dir_mypy(mod):
inner_class = next(
d for d in mod.iterdir() if d.is_dir() and not str(d).endswith("egg-info")
)
inner_class = next(d for d in mod.iterdir() if d.is_dir() and not str(d).endswith("egg-info"))
try:
check_call(
[
Expand All @@ -37,9 +35,7 @@ def _single_dir_mypy(mod):
)
return True
except CalledProcessError as e:
logging.error(
"{} exited with mypy error {}".format(inner_class.stem, e.returncode)
)
logging.error("{} exited with mypy error {}".format(inner_class.stem, e.returncode))
return False


Expand Down
8 changes: 2 additions & 6 deletions eng/scripts/run_pylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@


def _single_dir_pylint(mod):
inner_class = next(
d for d in mod.iterdir() if d.is_dir() and not str(d).endswith("egg-info")
)
inner_class = next(d for d in mod.iterdir() if d.is_dir() and not str(d).endswith("egg-info"))
try:
check_call(
[
Expand All @@ -37,9 +35,7 @@ def _single_dir_pylint(mod):
)
return True
except CalledProcessError as e:
logging.error(
"{} exited with linting error {}".format(inner_class.stem, e.returncode)
)
logging.error("{} exited with linting error {}".format(inner_class.stem, e.returncode))
return False


Expand Down
8 changes: 2 additions & 6 deletions eng/scripts/run_pyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@


def _single_dir_pyright(mod):
inner_class = next(
d for d in mod.iterdir() if d.is_dir() and not str(d).endswith("egg-info")
)
inner_class = next(d for d in mod.iterdir() if d.is_dir() and not str(d).endswith("egg-info"))
retries = 3
while retries:
try:
Expand All @@ -37,9 +35,7 @@ def _single_dir_pyright(mod):
)
return True
except CalledProcessError as e:
logging.exception(
"{} exited with pyright error {}".format(inner_class.stem, e.returncode)
)
logging.exception("{} exited with pyright error {}".format(inner_class.stem, e.returncode))
logging.error(f"PyRight stdout:\n{e.stdout}\n===========")
logging.error(f"PyRight stderr:\n{e.stderr}\n===========")
# PyRight has shown to randomly failed with a 217, retry the same folder 3 times should help
Expand Down
6 changes: 2 additions & 4 deletions eng/scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
root_dir = Path.resolve(Path(__file__) / "../../..")


def main(
package_name: str, regenerate: bool, check_change: bool, check_code: bool = False
):
def main(package_name: str, regenerate: bool, check_change: bool, check_code: bool = False):
if package_name == "autorest.python":
folder_name = "autorest"
elif package_name == "typespec-python":
Expand Down Expand Up @@ -49,7 +47,7 @@ def main(
# Pyright
call(f"pyright {folder_name}", cwd=package_dir)
# Black
call(f"black {folder_name}", cwd=package_dir)
call(f"black .", cwd=package_dir)
# Fail on black autorest diff
call("node ./eng/scripts/check-for-changed-files.js")
# Unit tests
Expand Down
13 changes: 2 additions & 11 deletions eng/scripts/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,12 @@ def run_check(name, call_back, log_info):

args = parser.parse_args()

pkg_dir = (
Path(ROOT_FOLDER)
/ Path(f"packages/{args.package}")
/ Path("test")
/ Path(args.test_folder)
)
pkg_dir = Path(ROOT_FOLDER) / Path(f"packages/{args.package}") / Path("test") / Path(args.test_folder)
if args.generator:
pkg_dir /= Path(args.generator)
if args.subfolder:
pkg_dir /= Path(args.subfolder)
dirs = [
d
for d in pkg_dir.iterdir()
if d.is_dir() and not d.stem.startswith("_") and d.stem not in IGNORE_FOLDER
]
dirs = [d for d in pkg_dir.iterdir() if d.is_dir() and not d.stem.startswith("_") and d.stem not in IGNORE_FOLDER]
if args.file_name:
dirs = [d for d in dirs if d.stem.lower() == args.file_name.lower()]
if len(dirs) > 1:
Expand Down
16 changes: 4 additions & 12 deletions packages/autorest.python/autorest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def __init__(self, *, output_folder: Union[str, Path], **kwargs: Any) -> None:
python_json = {}
self.options = kwargs
if python_json:
_LOGGER.warning(
"Loading python.json file. This behavior will be depreacted"
)
_LOGGER.warning("Loading python.json file. This behavior will be depreacted")
self.options.update(python_json)

def read_file(self, path: Union[str, Path]) -> str:
Expand All @@ -61,9 +59,7 @@ def list_file(self) -> List[str]:


class ReaderAndWriterAutorest(ReaderAndWriter):
def __init__(
self, *, output_folder: Union[str, Path], autorestapi: AutorestAPI
) -> None:
def __init__(self, *, output_folder: Union[str, Path], autorestapi: AutorestAPI) -> None:
super().__init__(output_folder=output_folder)
self._autorestapi = autorestapi

Expand Down Expand Up @@ -97,9 +93,7 @@ def process(self) -> bool:
class PluginAutorest(Plugin, ReaderAndWriterAutorest):
"""For our Autorest plugins, we want to take autorest api as input as options, then pass it to the Plugin"""

def __init__(
self, autorestapi: AutorestAPI, *, output_folder: Union[str, Path]
) -> None:
def __init__(self, autorestapi: AutorestAPI, *, output_folder: Union[str, Path]) -> None:
super().__init__(autorestapi=autorestapi, output_folder=output_folder)
self.options = self.get_options()

Expand Down Expand Up @@ -141,9 +135,7 @@ def update_yaml(self, yaml_data: Dict[str, Any]) -> None:
raise NotImplementedError()


class YamlUpdatePluginAutorest( # pylint: disable=abstract-method
YamlUpdatePlugin, PluginAutorest
):
class YamlUpdatePluginAutorest(YamlUpdatePlugin, PluginAutorest): # pylint: disable=abstract-method
def get_yaml(self) -> Dict[str, Any]:
return yaml.safe_load(self.read_file("code-model-v4-no-tags.yaml"))

Expand Down
25 changes: 5 additions & 20 deletions packages/autorest.python/autorest/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import argparse


def update_enum_value(
name: str, value: Any, description: str, enum_type: Dict[str, Any]
) -> Dict[str, Any]:
def update_enum_value(name: str, value: Any, description: str, enum_type: Dict[str, Any]) -> Dict[str, Any]:
return {
"name": name,
"type": "enumvalue",
Expand All @@ -37,12 +35,7 @@ def replace_upper_characters(m) -> str:
and len(name) - next_non_upper_case_char_location > 1
and name[next_non_upper_case_char_location].isalpha()
):
return (
prefix
+ match_str[: len(match_str) - 1]
+ "_"
+ match_str[len(match_str) - 1]
)
return prefix + match_str[: len(match_str) - 1] + "_" + match_str[len(match_str) - 1]

return prefix + match_str

Expand Down Expand Up @@ -89,12 +82,8 @@ def _get_value(value: Any) -> Any:
return value

unknown_args_ret = {
ua.strip("--").split("=", maxsplit=1)[ # pylint: disable=bad-str-strip-call
0
]: _get_value(
ua.strip("--").split("=", maxsplit=1)[ # pylint: disable=bad-str-strip-call
1
]
ua.strip("--").split("=", maxsplit=1)[0]: _get_value( # pylint: disable=bad-str-strip-call
ua.strip("--").split("=", maxsplit=1)[1] # pylint: disable=bad-str-strip-call
)
for ua in unknown_args
}
Expand Down Expand Up @@ -136,11 +125,7 @@ def build_policies(
"self._config.user_agent_policy",
"self._config.proxy_policy",
"policies.ContentDecodePolicy(**kwargs)",
(
f"{async_prefix}ARMAutoResourceProviderRegistrationPolicy()"
if is_arm
else None
),
(f"{async_prefix}ARMAutoResourceProviderRegistrationPolicy()" if is_arm else None),
"self._config.redirect_policy",
"self._config.retry_policy",
"self._config.authentication_policy",
Expand Down
4 changes: 1 addition & 3 deletions packages/autorest.python/autorest/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def process(self) -> bool:
def format_file(self, file: Path) -> None:
try:
file_content = self.read_file(file)
file_content = black.format_file_contents(
file_content, fast=True, mode=_BLACK_MODE
)
file_content = black.format_file_contents(file_content, fast=True, mode=_BLACK_MODE)
except NothingChanged:
pass
except: # pylint: disable=bare-except
Expand Down
Loading

0 comments on commit adfeb94

Please sign in to comment.