| 1 |
from functools import partial |
| 2 |
|
| 3 |
from ..utils import isort_test |
| 4 |
|
| 5 |
google_isort_test = partial(isort_test, profile="google") |
| 6 |
|
| 7 |
|
| 8 |
def test_google_code_snippet_shared_example() -> None: |
| 9 |
"""Tests snippet examples directly shared with the isort project. |
| 10 |
See: https://github.com/PyCQA/isort/issues/1486. |
| 11 |
""" |
| 12 |
google_isort_test( |
| 13 |
"""import collections |
| 14 |
import cProfile |
| 15 |
""" |
| 16 |
) |
| 17 |
google_isort_test( |
| 18 |
"""from a import z |
| 19 |
from a.b import c |
| 20 |
from a.b import import_me |
| 21 |
""" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
def test_google_code_snippet_one() -> None: |
| 26 |
google_isort_test( |
| 27 |
'''# coding=utf-8 |
| 28 |
# Copyright 2018 Google LLC |
| 29 |
# |
| 30 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 31 |
# you may not use this file except in compliance with the License. |
| 32 |
# You may obtain a copy of the License at |
| 33 |
# |
| 34 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 35 |
# |
| 36 |
# Unless required by applicable law or agreed to in writing, software |
| 37 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 38 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 |
# See the License for the specific language governing permissions and |
| 40 |
# limitations under the License. |
| 41 |
"""JAX user-facing transformations and utilities. |
| 42 |
The transformations here mostly wrap internal transformations, providing |
| 43 |
convenience flags to control behavior and handling Python containers of |
| 44 |
arguments and outputs. The Python containers handled are pytrees (see |
| 45 |
tree_util.py), which include nested tuples/lists/dicts, where the leaves are |
| 46 |
arrays. |
| 47 |
""" |
| 48 |
|
| 49 |
# flake8: noqa: F401 |
| 50 |
import collections |
| 51 |
import functools |
| 52 |
import inspect |
| 53 |
import itertools as it |
| 54 |
import threading |
| 55 |
import weakref |
| 56 |
from typing import Any, Callable, Iterable, List, NamedTuple, Optional, Sequence, Tuple, TypeVar, Union |
| 57 |
from warnings import warn |
| 58 |
|
| 59 |
import numpy as np |
| 60 |
from contextlib import contextmanager, ExitStack |
| 61 |
|
| 62 |
from . import core |
| 63 |
from . import linear_util as lu |
| 64 |
from . import ad_util |
| 65 |
from . import dtypes |
| 66 |
from .core import eval_jaxpr |
| 67 |
from .api_util import (wraps, flatten_fun, apply_flat_fun, flatten_fun_nokwargs, |
| 68 |
flatten_fun_nokwargs2, argnums_partial, flatten_axes, |
| 69 |
donation_vector, rebase_donate_argnums) |
| 70 |
from .traceback_util import api_boundary |
| 71 |
from .tree_util import (tree_map, tree_flatten, tree_unflatten, tree_structure, |
| 72 |
tree_transpose, tree_leaves, tree_multimap, |
| 73 |
treedef_is_leaf, Partial) |
| 74 |
from .util import (unzip2, curry, partial, safe_map, safe_zip, prod, split_list, |
| 75 |
extend_name_stack, wrap_name, cache) |
| 76 |
from .lib import xla_bridge as xb |
| 77 |
from .lib import xla_client as xc |
| 78 |
# Unused imports to be exported |
| 79 |
from .lib.xla_bridge import (device_count, local_device_count, devices, |
| 80 |
local_devices, host_id, host_ids, host_count) |
| 81 |
from .abstract_arrays import ConcreteArray, ShapedArray, raise_to_shaped |
| 82 |
from .interpreters import partial_eval as pe |
| 83 |
from .interpreters import xla |
| 84 |
from .interpreters import pxla |
| 85 |
from .interpreters import ad |
| 86 |
from .interpreters import batching |
| 87 |
from .interpreters import masking |
| 88 |
from .interpreters import invertible_ad as iad |
| 89 |
from .interpreters.invertible_ad import custom_ivjp |
| 90 |
from .custom_derivatives import custom_jvp, custom_vjp |
| 91 |
from .config import flags, config, bool_env |
| 92 |
|
| 93 |
AxisName = Any |
| 94 |
|
| 95 |
# This TypeVar is used below to express the fact that function call signatures |
| 96 |
# are invariant under the jit, vmap, and pmap transformations. |
| 97 |
# Specifically, we statically assert that the return type is invariant. |
| 98 |
# Until PEP-612 is implemented, we cannot express the same invariance for |
| 99 |
# function arguments. |
| 100 |
# Note that the return type annotations will generally not strictly hold |
| 101 |
# in JIT internals, as Tracer values are passed through the function. |
| 102 |
# Should this raise any type errors for the tracing code in future, we can disable |
| 103 |
# type checking in parts of the tracing code, or remove these annotations. |
| 104 |
T = TypeVar("T") |
| 105 |
|
| 106 |
map = safe_map |
| 107 |
zip = safe_zip |
| 108 |
|
| 109 |
FLAGS = flags.FLAGS |
| 110 |
flags.DEFINE_bool("jax_disable_jit", bool_env("JAX_DISABLE_JIT", False), |
| 111 |
"Disable JIT compilation and just call original Python.") |
| 112 |
|
| 113 |
''', |
| 114 |
'''# coding=utf-8 |
| 115 |
# Copyright 2018 Google LLC |
| 116 |
# |
| 117 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 118 |
# you may not use this file except in compliance with the License. |
| 119 |
# You may obtain a copy of the License at |
| 120 |
# |
| 121 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 122 |
# |
| 123 |
# Unless required by applicable law or agreed to in writing, software |
| 124 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 125 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 126 |
# See the License for the specific language governing permissions and |
| 127 |
# limitations under the License. |
| 128 |
"""JAX user-facing transformations and utilities. |
| 129 |
The transformations here mostly wrap internal transformations, providing |
| 130 |
convenience flags to control behavior and handling Python containers of |
| 131 |
arguments and outputs. The Python containers handled are pytrees (see |
| 132 |
tree_util.py), which include nested tuples/lists/dicts, where the leaves are |
| 133 |
arrays. |
| 134 |
""" |
| 135 |
|
| 136 |
# flake8: noqa: F401 |
| 137 |
import collections |
| 138 |
from contextlib import contextmanager |
| 139 |
from contextlib import ExitStack |
| 140 |
import functools |
| 141 |
import inspect |
| 142 |
import itertools as it |
| 143 |
import threading |
| 144 |
from typing import Any, Callable, Iterable, List, NamedTuple, Optional, Sequence, Tuple, TypeVar, Union |
| 145 |
from warnings import warn |
| 146 |
import weakref |
| 147 |
|
| 148 |
import numpy as np |
| 149 |
|
| 150 |
from . import ad_util |
| 151 |
from . import core |
| 152 |
from . import dtypes |
| 153 |
from . import linear_util as lu |
| 154 |
from .abstract_arrays import ConcreteArray |
| 155 |
from .abstract_arrays import raise_to_shaped |
| 156 |
from .abstract_arrays import ShapedArray |
| 157 |
from .api_util import apply_flat_fun |
| 158 |
from .api_util import argnums_partial |
| 159 |
from .api_util import donation_vector |
| 160 |
from .api_util import flatten_axes |
| 161 |
from .api_util import flatten_fun |
| 162 |
from .api_util import flatten_fun_nokwargs |
| 163 |
from .api_util import flatten_fun_nokwargs2 |
| 164 |
from .api_util import rebase_donate_argnums |
| 165 |
from .api_util import wraps |
| 166 |
from .config import bool_env |
| 167 |
from .config import config |
| 168 |
from .config import flags |
| 169 |
from .core import eval_jaxpr |
| 170 |
from .custom_derivatives import custom_jvp |
| 171 |
from .custom_derivatives import custom_vjp |
| 172 |
from .interpreters import ad |
| 173 |
from .interpreters import batching |
| 174 |
from .interpreters import invertible_ad as iad |
| 175 |
from .interpreters import masking |
| 176 |
from .interpreters import partial_eval as pe |
| 177 |
from .interpreters import pxla |
| 178 |
from .interpreters import xla |
| 179 |
from .interpreters.invertible_ad import custom_ivjp |
| 180 |
from .lib import xla_bridge as xb |
| 181 |
from .lib import xla_client as xc |
| 182 |
# Unused imports to be exported |
| 183 |
from .lib.xla_bridge import device_count |
| 184 |
from .lib.xla_bridge import devices |
| 185 |
from .lib.xla_bridge import host_count |
| 186 |
from .lib.xla_bridge import host_id |
| 187 |
from .lib.xla_bridge import host_ids |
| 188 |
from .lib.xla_bridge import local_device_count |
| 189 |
from .lib.xla_bridge import local_devices |
| 190 |
from .traceback_util import api_boundary |
| 191 |
from .tree_util import Partial |
| 192 |
from .tree_util import tree_flatten |
| 193 |
from .tree_util import tree_leaves |
| 194 |
from .tree_util import tree_map |
| 195 |
from .tree_util import tree_multimap |
| 196 |
from .tree_util import tree_structure |
| 197 |
from .tree_util import tree_transpose |
| 198 |
from .tree_util import tree_unflatten |
| 199 |
from .tree_util import treedef_is_leaf |
| 200 |
from .util import cache |
| 201 |
from .util import curry |
| 202 |
from .util import extend_name_stack |
| 203 |
from .util import partial |
| 204 |
from .util import prod |
| 205 |
from .util import safe_map |
| 206 |
from .util import safe_zip |
| 207 |
from .util import split_list |
| 208 |
from .util import unzip2 |
| 209 |
from .util import wrap_name |
| 210 |
|
| 211 |
AxisName = Any |
| 212 |
|
| 213 |
# This TypeVar is used below to express the fact that function call signatures |
| 214 |
# are invariant under the jit, vmap, and pmap transformations. |
| 215 |
# Specifically, we statically assert that the return type is invariant. |
| 216 |
# Until PEP-612 is implemented, we cannot express the same invariance for |
| 217 |
# function arguments. |
| 218 |
# Note that the return type annotations will generally not strictly hold |
| 219 |
# in JIT internals, as Tracer values are passed through the function. |
| 220 |
# Should this raise any type errors for the tracing code in future, we can disable |
| 221 |
# type checking in parts of the tracing code, or remove these annotations. |
| 222 |
T = TypeVar("T") |
| 223 |
|
| 224 |
map = safe_map |
| 225 |
zip = safe_zip |
| 226 |
|
| 227 |
FLAGS = flags.FLAGS |
| 228 |
flags.DEFINE_bool("jax_disable_jit", bool_env("JAX_DISABLE_JIT", False), |
| 229 |
"Disable JIT compilation and just call original Python.") |
| 230 |
|
| 231 |
''', |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
def test_google_code_snippet_two() -> None: |
| 236 |
google_isort_test( |
| 237 |
"""#!/usr/bin/env python |
| 238 |
# In[ ]: |
| 239 |
# coding: utf-8 |
| 240 |
|
| 241 |
###### Searching and Downloading Google Images to the local disk ###### |
| 242 |
|
| 243 |
# Import Libraries |
| 244 |
import sys |
| 245 |
version = (3, 0) |
| 246 |
cur_version = sys.version_info |
| 247 |
if cur_version >= version: # If the Current Version of Python is 3.0 or above |
| 248 |
import urllib.request |
| 249 |
from urllib.request import Request, urlopen |
| 250 |
from urllib.request import URLError, HTTPError |
| 251 |
from urllib.parse import quote |
| 252 |
import http.client |
| 253 |
from http.client import IncompleteRead, BadStatusLine |
| 254 |
http.client._MAXHEADERS = 1000 |
| 255 |
else: # If the Current Version of Python is 2.x |
| 256 |
import urllib2 |
| 257 |
from urllib2 import Request, urlopen |
| 258 |
from urllib2 import URLError, HTTPError |
| 259 |
from urllib import quote |
| 260 |
import httplib |
| 261 |
from httplib import IncompleteRead, BadStatusLine |
| 262 |
httplib._MAXHEADERS = 1000 |
| 263 |
import time # Importing the time library to check the time of code execution |
| 264 |
import os |
| 265 |
import argparse |
| 266 |
import ssl |
| 267 |
import datetime |
| 268 |
import json |
| 269 |
import re |
| 270 |
import codecs |
| 271 |
import socket""", |
| 272 |
"""#!/usr/bin/env python |
| 273 |
# In[ ]: |
| 274 |
# coding: utf-8 |
| 275 |
|
| 276 |
###### Searching and Downloading Google Images to the local disk ###### |
| 277 |
|
| 278 |
# Import Libraries |
| 279 |
import sys |
| 280 |
|
| 281 |
version = (3, 0) |
| 282 |
cur_version = sys.version_info |
| 283 |
if cur_version >= version: # If the Current Version of Python is 3.0 or above |
| 284 |
import http.client |
| 285 |
from http.client import BadStatusLine |
| 286 |
from http.client import IncompleteRead |
| 287 |
from urllib.parse import quote |
| 288 |
import urllib.request |
| 289 |
from urllib.request import HTTPError |
| 290 |
from urllib.request import Request |
| 291 |
from urllib.request import URLError |
| 292 |
from urllib.request import urlopen |
| 293 |
http.client._MAXHEADERS = 1000 |
| 294 |
else: # If the Current Version of Python is 2.x |
| 295 |
from urllib import quote |
| 296 |
|
| 297 |
import httplib |
| 298 |
from httplib import BadStatusLine |
| 299 |
from httplib import IncompleteRead |
| 300 |
import urllib2 |
| 301 |
from urllib2 import HTTPError |
| 302 |
from urllib2 import Request |
| 303 |
from urllib2 import URLError |
| 304 |
from urllib2 import urlopen |
| 305 |
httplib._MAXHEADERS = 1000 |
| 306 |
import argparse |
| 307 |
import codecs |
| 308 |
import datetime |
| 309 |
import json |
| 310 |
import os |
| 311 |
import re |
| 312 |
import socket |
| 313 |
import ssl |
| 314 |
import time # Importing the time library to check the time of code execution |
| 315 |
""", |
| 316 |
) |
| 317 |
|
| 318 |
|
| 319 |
def test_code_snippet_three() -> None: |
| 320 |
google_isort_test( |
| 321 |
'''# Copyright 2019 Google LLC |
| 322 |
# |
| 323 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 324 |
# you may not use this file except in compliance with the License. |
| 325 |
# You may obtain a copy of the License at |
| 326 |
# |
| 327 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 328 |
# |
| 329 |
# Unless required by applicable law or agreed to in writing, software |
| 330 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 331 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 332 |
# See the License for the specific language governing permissions and |
| 333 |
# limitations under the License. |
| 334 |
"""Monitoring.""" |
| 335 |
# pylint: disable=invalid-name |
| 336 |
# TODO(ochang): Remove V3 from names once all metrics are migrated to |
| 337 |
# stackdriver. |
| 338 |
|
| 339 |
from builtins import object |
| 340 |
from builtins import range |
| 341 |
from builtins import str |
| 342 |
|
| 343 |
import bisect |
| 344 |
import collections |
| 345 |
import functools |
| 346 |
import itertools |
| 347 |
import re |
| 348 |
import six |
| 349 |
import threading |
| 350 |
import time |
| 351 |
|
| 352 |
try: |
| 353 |
from google.cloud import monitoring_v3 |
| 354 |
except (ImportError, RuntimeError): |
| 355 |
monitoring_v3 = None |
| 356 |
|
| 357 |
from google.api_core import exceptions |
| 358 |
from google.api_core import retry |
| 359 |
|
| 360 |
from base import errors |
| 361 |
from base import utils |
| 362 |
from config import local_config |
| 363 |
from google_cloud_utils import compute_metadata |
| 364 |
from google_cloud_utils import credentials |
| 365 |
from metrics import logs |
| 366 |
from system import environment''', |
| 367 |
'''# Copyright 2019 Google LLC |
| 368 |
# |
| 369 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 370 |
# you may not use this file except in compliance with the License. |
| 371 |
# You may obtain a copy of the License at |
| 372 |
# |
| 373 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 374 |
# |
| 375 |
# Unless required by applicable law or agreed to in writing, software |
| 376 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 377 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 378 |
# See the License for the specific language governing permissions and |
| 379 |
# limitations under the License. |
| 380 |
"""Monitoring.""" |
| 381 |
# pylint: disable=invalid-name |
| 382 |
# TODO(ochang): Remove V3 from names once all metrics are migrated to |
| 383 |
# stackdriver. |
| 384 |
|
| 385 |
import bisect |
| 386 |
from builtins import object |
| 387 |
from builtins import range |
| 388 |
from builtins import str |
| 389 |
import collections |
| 390 |
import functools |
| 391 |
import itertools |
| 392 |
import re |
| 393 |
import threading |
| 394 |
import time |
| 395 |
|
| 396 |
import six |
| 397 |
|
| 398 |
try: |
| 399 |
from google.cloud import monitoring_v3 |
| 400 |
except (ImportError, RuntimeError): |
| 401 |
monitoring_v3 = None |
| 402 |
|
| 403 |
from base import errors |
| 404 |
from base import utils |
| 405 |
from config import local_config |
| 406 |
from google.api_core import exceptions |
| 407 |
from google.api_core import retry |
| 408 |
from google_cloud_utils import compute_metadata |
| 409 |
from google_cloud_utils import credentials |
| 410 |
from metrics import logs |
| 411 |
from system import environment |
| 412 |
''', |
| 413 |
) |