Skip to content

Commit e12b89d

Browse files
author
Mattia Baldari
committed
First ruff fix
1 parent 369496d commit e12b89d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+560
-541
lines changed

schemadiff/__init__.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
1-
from typing import Union, List
1+
from typing import List, Union
22

3-
from graphql import GraphQLSchema as GQLSchema, is_schema
3+
from graphql import GraphQLSchema as GQLSchema
4+
from graphql import is_schema
45

56
from schemadiff.changes import Change
67
from schemadiff.diff.schema import Schema
8+
from schemadiff.formatting import format_diff, print_diff
79
from schemadiff.schema_loader import SchemaLoader
8-
from schemadiff.formatting import print_diff, format_diff
910
from schemadiff.validation import validate_changes
1011

11-
1212
SDL = str # Alias for string describing schema through schema definition language
1313

1414

15-
def diff(old_schema: Union[SDL, GQLSchema], new_schema: Union[SDL, GQLSchema]) -> List[Change]:
16-
"""Compare two graphql schemas highlighting dangerous and breaking changes.
15+
def diff(old_schema: SDL | GQLSchema, new_schema: SDL | GQLSchema) -> list[Change]:
16+
"""
17+
Compare two graphql schemas highlighting dangerous and breaking changes.
1718
1819
Returns:
1920
changes (List[Change]): List of differences between both schemas with details about each change
21+
2022
"""
2123
first = SchemaLoader.from_sdl(old_schema) if not is_schema(old_schema) else old_schema
2224
second = SchemaLoader.from_sdl(new_schema) if not is_schema(new_schema) else new_schema
2325
return Schema(first, second).diff()
2426

2527

2628
def diff_from_file(schema_file: str, other_schema_file: str):
27-
"""Compare two graphql schema files highlighting dangerous and breaking changes.
29+
"""
30+
Compare two graphql schema files highlighting dangerous and breaking changes.
2831
2932
Returns:
3033
changes (List[Change]): List of differences between both schemas with details about each change
34+
3135
"""
3236
first = SchemaLoader.from_file(schema_file)
3337
second = SchemaLoader.from_file(other_schema_file)
3438
return Schema(first, second).diff()
3539

3640

3741
__all__ = [
38-
'diff',
39-
'diff_from_file',
40-
'format_diff',
41-
'print_diff',
42-
'validate_changes',
43-
'Change',
42+
"Change",
43+
"diff",
44+
"diff_from_file",
45+
"format_diff",
46+
"print_diff",
47+
"validate_changes",
4448
]

schemadiff/__main__.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import sys
21
import argparse
2+
import sys
33

44
from schemadiff.allow_list import read_allowed_changes
55
from schemadiff.diff.schema import Schema
6-
from schemadiff.schema_loader import SchemaLoader
76
from schemadiff.formatting import print_diff, print_json
7+
from schemadiff.schema_loader import SchemaLoader
88
from schemadiff.validation import rules_list, validate_changes
99

1010

@@ -14,31 +14,31 @@ def cli():
1414

1515

1616
def parse_args(arguments):
17-
parser = argparse.ArgumentParser(description='Schema comparator')
18-
parser.add_argument('-o', '--old-schema',
19-
dest='old_schema',
20-
type=argparse.FileType('r', encoding='UTF-8'),
21-
help='Path to old graphql schema file',
17+
parser = argparse.ArgumentParser(description="Schema comparator")
18+
parser.add_argument("-o", "--old-schema",
19+
dest="old_schema",
20+
type=argparse.FileType("r", encoding="UTF-8"),
21+
help="Path to old graphql schema file",
2222
required=True)
23-
parser.add_argument('-n', '--new-schema',
24-
dest='new_schema',
25-
type=argparse.FileType('r', encoding='UTF-8'),
26-
help='Path to new graphql schema file',
23+
parser.add_argument("-n", "--new-schema",
24+
dest="new_schema",
25+
type=argparse.FileType("r", encoding="UTF-8"),
26+
help="Path to new graphql schema file",
2727
required=True)
28-
parser.add_argument('-j', '--as-json',
29-
action='store_true',
30-
help='Output a detailed summary of changes in json format',
28+
parser.add_argument("-j", "--as-json",
29+
action="store_true",
30+
help="Output a detailed summary of changes in json format",
3131
required=False)
32-
parser.add_argument('-a', '--allow-list',
33-
type=argparse.FileType('r', encoding='UTF-8'),
34-
help='Path to the allowed list of changes')
35-
parser.add_argument('-t', '--tolerant',
36-
action='store_true',
32+
parser.add_argument("-a", "--allow-list",
33+
type=argparse.FileType("r", encoding="UTF-8"),
34+
help="Path to the allowed list of changes")
35+
parser.add_argument("-t", "--tolerant",
36+
action="store_true",
3737
help="Tolerant mode. Error out only if there's a breaking change but allow dangerous changes")
38-
parser.add_argument('-s', '--strict',
39-
action='store_true',
38+
parser.add_argument("-s", "--strict",
39+
action="store_true",
4040
help="Strict mode. Error out on dangerous and breaking changes.")
41-
parser.add_argument('-r', '--validation-rules', choices=rules_list(), nargs='*',
41+
parser.add_argument("-r", "--validation-rules", choices=rules_list(), nargs="*",
4242
help="Evaluate rules mode. Error out on changes that fail some validation rule.")
4343

4444
return parser.parse_args(arguments)
@@ -79,5 +79,5 @@ def exit_code(changes, strict, some_change_is_restricted, tolerant) -> int:
7979
return exit_code
8080

8181

82-
if __name__ == '__main__':
82+
if __name__ == "__main__":
8383
sys.exit(cli())

schemadiff/allow_list.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class InvalidAllowlist(Exception):
88

99

1010
def read_allowed_changes(file_content):
11-
"""Read a json file that defines a mapping of changes checksums to the reasons of why it is allowed.
11+
"""
12+
Read a json file that defines a mapping of changes checksums to the reasons of why it is allowed.
1213
1314
Compliant formats:
1415
{
@@ -36,7 +37,7 @@ def read_allowed_changes(file_content):
3637
if not isinstance(allowlist, dict):
3738
raise InvalidAllowlist("Allowlist must be a mapping.")
3839

39-
CHECKSUM_REGEX = re.compile(r'[a-fA-F0-9]{32}')
40+
CHECKSUM_REGEX = re.compile(r"[a-fA-F0-9]{32}")
4041
if any(not CHECKSUM_REGEX.match(checksum) for checksum in allowlist.keys()):
4142
raise InvalidAllowlist("All keys must be a valid md5 checksum")
4243

schemadiff/changes/__init__.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import hashlib
22
import json
3-
from abc import abstractmethod, ABC
3+
from abc import ABC, abstractmethod
44
from enum import Enum
55
from typing import Optional
66

77
from attr import dataclass
8-
from graphql import is_wrapping_type, is_non_null_type, is_list_type
8+
from graphql import is_list_type, is_non_null_type, is_wrapping_type
99

1010

1111
class CriticalityLevel(Enum):
12-
NonBreaking = 'NON_BREAKING'
13-
Dangerous = 'DANGEROUS'
14-
Breaking = 'BREAKING'
12+
NonBreaking = "NON_BREAKING"
13+
Dangerous = "DANGEROUS"
14+
Breaking = "BREAKING"
1515

1616

1717
@dataclass(repr=False)
@@ -40,7 +40,8 @@ def __repr__(self):
4040

4141

4242
def is_safe_type_change(old_type, new_type) -> bool:
43-
"""Depending on the old an new type, a field type change may be breaking, dangerous or safe
43+
"""
44+
Depending on the old an new type, a field type change may be breaking, dangerous or safe
4445
4546
* If both fields are 'leafs' in the sense they don't wrap an inner type, just compare their type.
4647
* If the new type has a non-null constraint, check that it was already non-null and compare against the contained
@@ -86,7 +87,8 @@ def is_safe_change_for_input_value(old_type, new_type):
8687

8788

8889
class Change(ABC):
89-
"""Common interface of all schema changes
90+
"""
91+
Common interface of all schema changes
9092
9193
This class offers the common operations and properties of all
9294
schema changes. You may use it as a type hint to get better
@@ -95,7 +97,7 @@ class Change(ABC):
9597

9698
criticality: Criticality = None
9799

98-
restricted: Optional[str] = None
100+
restricted: str | None = None
99101
"""Descriptive message only present when a change was restricted"""
100102

101103
@property
@@ -132,14 +134,14 @@ def __str__(self) -> str:
132134
def to_dict(self) -> dict:
133135
"""Get detailed representation of a change"""
134136
return {
135-
'message': self.message,
136-
'path': self.path,
137-
'is_safe_change': self.safe,
138-
'criticality': {
139-
'level': self.criticality.level.value,
140-
'reason': self.criticality.reason
137+
"message": self.message,
138+
"path": self.path,
139+
"is_safe_change": self.safe,
140+
"criticality": {
141+
"level": self.criticality.level.value,
142+
"reason": self.criticality.reason
141143
},
142-
'checksum': self.checksum(),
144+
"checksum": self.checksum(),
143145
}
144146

145147
def to_json(self) -> str:
@@ -148,4 +150,4 @@ def to_json(self) -> str:
148150

149151
def checksum(self) -> str:
150152
"""Get and identifier of a change. Used for allowlisting changes"""
151-
return hashlib.md5(self.message.encode('utf-8')).hexdigest()
153+
return hashlib.md5(self.message.encode("utf-8")).hexdigest()

schemadiff/changes/directive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, directive, directive_locations):
2020

2121
@property
2222
def message(self):
23-
locations = ' | '.join(loc.name for loc in self.directive_locations)
23+
locations = " | ".join(loc.name for loc in self.directive_locations)
2424
return f"Directive `{self.directive}` was added to use on `{locations}`"
2525

2626

schemadiff/changes/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ def message(self):
9191

9292
@property
9393
def path(self):
94-
return f"{self.enum.name}.{self.name}"
94+
return f"{self.enum.name}.{self.name}"

schemadiff/changes/field.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from graphql import is_non_null_type, GraphQLField
1+
from graphql import GraphQLField, is_non_null_type
22

33
from schemadiff.changes import Change, Criticality, is_safe_type_change
44

@@ -22,7 +22,7 @@ def message(self):
2222

2323
@property
2424
def path(self):
25-
return f'{self.type}.{self.field_name}'
25+
return f"{self.type}.{self.field_name}"
2626

2727

2828
class FieldDeprecationReasonChanged(Change):
@@ -52,7 +52,7 @@ class FieldTypeChanged(Change):
5252
def __init__(self, type_, field_name, old_field, new_field):
5353
self.criticality = Criticality.safe()\
5454
if is_safe_type_change(old_field.type, new_field.type)\
55-
else Criticality.breaking('Changing a field type will break queries that assume its type')
55+
else Criticality.breaking("Changing a field type will break queries that assume its type")
5656
self.type_ = type_
5757
self.field_name = field_name
5858
self.old_field = old_field
@@ -72,7 +72,7 @@ def path(self):
7272

7373
class FieldArgumentAdded(Change):
7474
def __init__(self, parent, field_name: str, field: GraphQLField, argument_name, arg_type):
75-
self.criticality = Criticality.safe('Adding an optional argument is a safe change')\
75+
self.criticality = Criticality.safe("Adding an optional argument is a safe change")\
7676
if not is_non_null_type(arg_type.type)\
7777
else Criticality.breaking("Adding a required argument to an existing field is a breaking "
7878
"change because it will break existing uses of this field")

schemadiff/changes/input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def path(self):
3434
class InputFieldRemoved(Change):
3535

3636
criticality = Criticality.breaking(
37-
'Removing an input field will break queries that use this input field.'
37+
"Removing an input field will break queries that use this input field."
3838
)
3939

4040
def __init__(self, input_object, value):
@@ -120,4 +120,4 @@ def message(self):
120120

121121
@property
122122
def path(self):
123-
return f"{self.input_.name}.{self.name}"
123+
return f"{self.input_.name}.{self.name}"

schemadiff/changes/interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def message(self):
116116

117117

118118
class InterfaceFieldDeprecationReasonChanged(AbstractInterfaceChange):
119-
criticality = Criticality.breaking('Breaking change') # TODO: Improve this logic to check if it was deprecated before
119+
criticality = Criticality.breaking("Breaking change") # TODO: Improve this logic to check if it was deprecated before
120120

121121
@property
122122
def message(self):

schemadiff/changes/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class SchemaChange(Change):
5-
criticality = Criticality.breaking('Changing a root type is a breaking change')
5+
criticality = Criticality.breaking("Changing a root type is a breaking change")
66

77
def __init__(self, old_type, new_type):
88
self.old_type = old_type

0 commit comments

Comments
 (0)