mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-14 20:57:59 +00:00
This PR changes the empty collection factory call to Python literals:
- `list()` -> `[]`
- `tuple()` -> `()`
- `dict()` -> `{}`
The Python literals are more performant and safer. For example, the bytecode for building an empty dictionary:
```bash
$ python3 -m dis - <<EOS
import collections
d1 = {}
d2 = dict()
dict = collections.OrderedDict
d3 = dict()
EOS
```
```text
0 0 RESUME 0
1 2 LOAD_CONST 0 (0)
4 LOAD_CONST 1 (None)
6 IMPORT_NAME 0 (collections)
8 STORE_NAME 0 (collections)
3 10 BUILD_MAP 0
12 STORE_NAME 1 (d1)
4 14 PUSH_NULL
16 LOAD_NAME 2 (dict)
18 CALL 0
26 STORE_NAME 3 (d2)
6 28 LOAD_NAME 0 (collections)
30 LOAD_ATTR 8 (OrderedDict)
50 STORE_NAME 2 (dict)
7 52 PUSH_NULL
54 LOAD_NAME 2 (dict)
56 CALL 0
64 STORE_NAME 5 (d3)
66 RETURN_CONST 1 (None)
```
The dict literal `{}` only has one bytecode `BUILD_MAP`, while the factory call `dict()` has three `PUSH_NULL + LOAD_NAME + CALL`. Also, the factory call is not safe if users override the `dict` name in `locals` or `globals` (see the example of replacing with `OrderedDict` above).
Pull Request resolved: https://github.com/pytorch/pytorch/pull/130199
Approved by: https://github.com/malfet
207 lines
5 KiB
TOML
207 lines
5 KiB
TOML
[build-system]
|
|
requires = [
|
|
"setuptools",
|
|
"wheel",
|
|
"astunparse",
|
|
"numpy",
|
|
"ninja",
|
|
"pyyaml",
|
|
"cmake",
|
|
"typing-extensions",
|
|
"requests",
|
|
]
|
|
# Use legacy backend to import local packages in setup.py
|
|
build-backend = "setuptools.build_meta:__legacy__"
|
|
|
|
|
|
[tool.black]
|
|
line-length = 88
|
|
target-version = ["py38", "py39", "py310", "py311"]
|
|
|
|
|
|
[tool.isort]
|
|
src_paths = ["caffe2", "torch", "torchgen", "functorch", "test"]
|
|
extra_standard_library = ["typing_extensions"]
|
|
skip_gitignore = true
|
|
skip_glob = ["third_party/*"]
|
|
atomic = true
|
|
profile = "black"
|
|
indent = 4
|
|
line_length = 88
|
|
lines_after_imports = 2
|
|
multi_line_output = 3
|
|
include_trailing_comma = true
|
|
combine_as_imports = true
|
|
|
|
|
|
[tool.usort.known]
|
|
first_party = ["caffe2", "torch", "torchgen", "functorch", "test"]
|
|
standard_library = ["typing_extensions"]
|
|
|
|
|
|
[tool.ruff]
|
|
target-version = "py38"
|
|
line-length = 120
|
|
|
|
[tool.ruff.lint]
|
|
# NOTE: Synchoronize the ignores with .flake8
|
|
ignore = [
|
|
# these ignores are from flake8-bugbear; please fix!
|
|
"B007", "B008", "B017",
|
|
"B018", # Useless expression
|
|
"B023",
|
|
"B028", # No explicit `stacklevel` keyword argument found
|
|
"E402",
|
|
"C408", # C408 ignored because we like the dict keyword argument syntax
|
|
"E501", # E501 is not flexible enough, we're using B950 instead
|
|
"E721",
|
|
"E731", # Assign lambda expression
|
|
"E741",
|
|
"EXE001",
|
|
"F405",
|
|
"F841",
|
|
# these ignores are from flake8-logging-format; please fix!
|
|
"G101",
|
|
# these ignores are from ruff NPY; please fix!
|
|
"NPY002",
|
|
# these ignores are from ruff PERF; please fix!
|
|
"PERF203",
|
|
"PERF401",
|
|
"PERF403",
|
|
# these ignores are from PYI; please fix!
|
|
"PYI024",
|
|
"PYI036",
|
|
"PYI041",
|
|
"PYI056",
|
|
"SIM102", "SIM103", "SIM112", # flake8-simplify code styles
|
|
"SIM113", # please fix
|
|
"SIM105", # these ignores are from flake8-simplify. please fix or ignore with commented reason
|
|
"SIM108", # SIM108 ignored because we prefer if-else-block instead of ternary expression
|
|
"SIM110",
|
|
"SIM114", # Combine `if` branches using logical `or` operator
|
|
"SIM115",
|
|
"SIM116", # Disable Use a dictionary instead of consecutive `if` statements
|
|
"SIM117",
|
|
"SIM118",
|
|
"UP006", # keep-runtime-typing
|
|
"UP007", # keep-runtime-typing
|
|
]
|
|
select = [
|
|
"B",
|
|
"B904", # Re-raised error without specifying the cause via the from keyword
|
|
"C4",
|
|
"G",
|
|
"E",
|
|
"EXE",
|
|
"F",
|
|
"SIM1",
|
|
"W",
|
|
# Not included in flake8
|
|
"FURB",
|
|
"LOG",
|
|
"NPY",
|
|
"PERF",
|
|
"PGH004",
|
|
"PIE794",
|
|
"PIE800",
|
|
"PIE804",
|
|
"PIE807",
|
|
"PIE810",
|
|
"PLC0131", # type bivariance
|
|
"PLC0132", # type param mismatch
|
|
"PLC0205", # string as __slots__
|
|
"PLE",
|
|
"PLR0133", # constant comparison
|
|
"PLR0206", # property with params
|
|
"PLR1722", # use sys exit
|
|
"PLR1736", # unnecessary list index
|
|
"PLW0129", # assert on string literal
|
|
"PLW0133", # useless exception statement
|
|
"PLW0406", # import self
|
|
"PLW0711", # binary op exception
|
|
"PLW1509", # preexec_fn not safe with threads
|
|
"PLW2101", # useless lock statement
|
|
"PLW3301", # nested min max
|
|
"PT006", # TODO: enable more PT rules
|
|
"PT022",
|
|
"PT023",
|
|
"PT024",
|
|
"PT025",
|
|
"PT026",
|
|
"PYI",
|
|
"Q003", # avoidable escaped quote
|
|
"Q004", # unnecessary escaped quote
|
|
"RSE",
|
|
"RUF008", # mutable dataclass default
|
|
"RUF015", # access first ele in constant time
|
|
"RUF016", # type error non-integer index
|
|
"RUF017",
|
|
"RUF018", # no assignment in assert
|
|
"RUF024", # from keys mutable
|
|
"RUF026", # default factory kwarg
|
|
"TCH",
|
|
"TRY002", # ban vanilla raise (todo fix NOQAs)
|
|
"TRY302",
|
|
"TRY401", # verbose-log-message
|
|
"UP",
|
|
]
|
|
|
|
[tool.ruff.lint.per-file-ignores]
|
|
"__init__.py" = [
|
|
"F401",
|
|
]
|
|
"functorch/notebooks/**" = [
|
|
"F401",
|
|
]
|
|
"test/typing/reveal/**" = [
|
|
"F821",
|
|
]
|
|
"test/torch_np/numpy_tests/**" = [
|
|
"F821",
|
|
"NPY201",
|
|
]
|
|
"test/dynamo/test_bytecode_utils.py" = [
|
|
"F821",
|
|
]
|
|
"test/dynamo/test_debug_utils.py" = [
|
|
"UP037",
|
|
]
|
|
"test/jit/**" = [
|
|
"PLR0133", # tests require this for JIT
|
|
"PYI",
|
|
"RUF015",
|
|
"UP", # We don't want to modify the jit test as they test specify syntax
|
|
]
|
|
"test/test_jit.py" = [
|
|
"PLR0133", # tests require this for JIT
|
|
"PYI",
|
|
"RUF015",
|
|
"UP", # We don't want to modify the jit test as they test specify syntax
|
|
]
|
|
"test/inductor/test_torchinductor.py" = [
|
|
"UP037",
|
|
]
|
|
# autogenerated #TODO figure out why file level noqa is ignored
|
|
"torch/_inductor/fx_passes/serialized_patterns/**" = ["F401", "F501"]
|
|
"torch/onnx/**" = [
|
|
"TCH001", # beartype may need runtime types
|
|
"TCH002",
|
|
"TCH003",
|
|
"TCH004",
|
|
"UP037", # ONNX does runtime type checking
|
|
]
|
|
|
|
"torchgen/api/types/__init__.py" = [
|
|
"F401",
|
|
"F403",
|
|
]
|
|
"torchgen/executorch/api/types/__init__.py" = [
|
|
"F401",
|
|
"F403",
|
|
]
|
|
"torch/utils/collect_env.py" = [
|
|
"UP", # collect_env.py needs to work with older versions of Python
|
|
]
|
|
"torch/_vendor/**" = [
|
|
"UP", # No need to mess with _vendor
|
|
]
|