From f45ad83b577bb3e4c46d750f0460986e1722c38a Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Wed, 17 Jun 2020 12:33:45 +0300 Subject: [PATCH 1/2] Use chain.from_iterable in class_validators.py --- changes/1642-cool-RR.txt | 3 +++ pydantic/class_validators.py | 8 +++----- 2 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 changes/1642-cool-RR.txt diff --git a/changes/1642-cool-RR.txt b/changes/1642-cool-RR.txt new file mode 100644 index 0000000000..0722a126d7 --- /dev/null +++ b/changes/1642-cool-RR.txt @@ -0,0 +1,3 @@ +Use chain.from_iterable in class_validators.py. + +This is a faster and more idiomatic way of using `itertools.chain`. Instead of computing all the items in the iterable and storing them in memory, they are computed one-by-one and never stored as a huge list. This can save on both runtime and memory space. \ No newline at end of file diff --git a/pydantic/class_validators.py b/pydantic/class_validators.py index 3d1032e789..cdcb221113 100644 --- a/pydantic/class_validators.py +++ b/pydantic/class_validators.py @@ -164,11 +164,9 @@ def get_validators(self, name: str) -> Optional[Dict[str, Validator]]: def check_for_unused(self) -> None: unused_validators = set( - chain( - *[ - (v.func.__name__ for v in self.validators[f] if v.check_fields) - for f in (self.validators.keys() - self.used_validators) - ] + chain.from_iterable( + (v.func.__name__ for v in self.validators[f] if v.check_fields) + for f in (self.validators.keys() - self.used_validators) ) ) if unused_validators: From 17029f318feabbc25614ddf3b9eadb8564b8e87a Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sat, 27 Jun 2020 15:37:38 +0100 Subject: [PATCH 2/2] fix change --- changes/1642-cool-RR.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/changes/1642-cool-RR.txt b/changes/1642-cool-RR.txt index 0722a126d7..5f4b611162 100644 --- a/changes/1642-cool-RR.txt +++ b/changes/1642-cool-RR.txt @@ -1,3 +1,3 @@ -Use chain.from_iterable in class_validators.py. - -This is a faster and more idiomatic way of using `itertools.chain`. Instead of computing all the items in the iterable and storing them in memory, they are computed one-by-one and never stored as a huge list. This can save on both runtime and memory space. \ No newline at end of file +Use `chain.from_iterable` in class_validators.py. This is a faster and more idiomatic way of using `itertools.chain`. +Instead of computing all the items in the iterable and storing them in memory, they are computed one-by-one and never +stored as a huge list. This can save on both runtime and memory space.