|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import sys |
3 | 4 | import tempfile |
4 | 5 | from argparse import Namespace |
5 | 6 | from collections import defaultdict |
@@ -114,11 +115,10 @@ class HelperClass: |
114 | 115 | def helper_method(self): |
115 | 116 | return self.name |
116 | 117 |
|
117 | | -
|
118 | 118 | class MainClass: |
119 | 119 |
|
120 | 120 | def main_method(self): |
121 | | - self.name = HelperClass.NestedClass("test").nested_method() |
| 121 | + self.name = HelperClass.NestedClass('test').nested_method() |
122 | 122 | return HelperClass(self.name).helper_method() |
123 | 123 | ``` |
124 | 124 | """ |
@@ -181,22 +181,17 @@ class Graph: |
181 | 181 |
|
182 | 182 | def topologicalSortUtil(self, v, visited, stack): |
183 | 183 | visited[v] = True |
184 | | -
|
185 | 184 | for i in self.graph[v]: |
186 | 185 | if visited[i] == False: |
187 | 186 | self.topologicalSortUtil(i, visited, stack) |
188 | | -
|
189 | 187 | stack.insert(0, v) |
190 | 188 |
|
191 | 189 | def topologicalSort(self): |
192 | 190 | visited = [False] * self.V |
193 | 191 | stack = [] |
194 | | -
|
195 | 192 | for i in range(self.V): |
196 | 193 | if visited[i] == False: |
197 | 194 | self.topologicalSortUtil(i, visited, stack) |
198 | | -
|
199 | | - # Print contents of stack |
200 | 195 | return stack |
201 | 196 | ``` |
202 | 197 | """ |
@@ -614,58 +609,37 @@ class _PersistentCache(Generic[_P, _R, _CacheBackendT]): |
614 | 609 | ```python:{file_path.relative_to(opt.args.project_root)} |
615 | 610 | class AbstractCacheBackend(CacheBackend, Protocol[_KEY_T, _STORE_T]): |
616 | 611 |
|
617 | | - def get_cache_or_call( |
618 | | - self, |
619 | | - *, |
620 | | - func: Callable[_P, Any], |
621 | | - args: tuple[Any, ...], |
622 | | - kwargs: dict[str, Any], |
623 | | - lifespan: datetime.timedelta, |
624 | | - ) -> Any: # noqa: ANN401 |
625 | | - if os.environ.get("NO_CACHE"): |
| 612 | + def get_cache_or_call(self, *, func: Callable[_P, Any], args: tuple[Any, ...], kwargs: dict[str, Any], lifespan: datetime.timedelta) -> Any: |
| 613 | + if os.environ.get('NO_CACHE'): |
626 | 614 | return func(*args, **kwargs) |
627 | | -
|
628 | 615 | try: |
629 | 616 | key = self.hash_key(func=func, args=args, kwargs=kwargs) |
630 | | - except: # noqa: E722 |
631 | | - # If we can't create a cache key, we should just call the function. |
632 | | - logging.warning("Failed to hash cache key for function: %s", func) |
| 617 | + except: |
| 618 | + logging.warning('Failed to hash cache key for function: %s', func) |
633 | 619 | return func(*args, **kwargs) |
634 | 620 | result_pair = self.get(key=key) |
635 | | -
|
636 | 621 | if result_pair is not None: |
637 | | - cached_time, result = result_pair |
638 | | - if not os.environ.get("RE_CACHE") and ( |
639 | | - datetime.datetime.now() < (cached_time + lifespan) # noqa: DTZ005 |
640 | | - ): |
| 622 | + {"cached_time, result = result_pair" if sys.version_info >= (3, 11) else "(cached_time, result) = result_pair"} |
| 623 | + if not os.environ.get('RE_CACHE') and datetime.datetime.now() < cached_time + lifespan: |
641 | 624 | try: |
642 | 625 | return self.decode(data=result) |
643 | 626 | except CacheBackendDecodeError as e: |
644 | | - logging.warning("Failed to decode cache data: %s", e) |
645 | | - # If decoding fails we will treat this as a cache miss. |
646 | | - # This might happens if underlying class definition of the data changes. |
| 627 | + logging.warning('Failed to decode cache data: %s', e) |
647 | 628 | self.delete(key=key) |
648 | 629 | result = func(*args, **kwargs) |
649 | 630 | try: |
650 | 631 | self.put(key=key, data=self.encode(data=result)) |
651 | 632 | except CacheBackendEncodeError as e: |
652 | | - logging.warning("Failed to encode cache data: %s", e) |
653 | | - # If encoding fails, we should still return the result. |
| 633 | + logging.warning('Failed to encode cache data: %s', e) |
654 | 634 | return result |
655 | 635 |
|
656 | | -
|
657 | 636 | class _PersistentCache(Generic[_P, _R, _CacheBackendT]): |
658 | 637 |
|
659 | 638 | def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: |
660 | | - if "NO_CACHE" in os.environ: |
| 639 | + if 'NO_CACHE' in os.environ: |
661 | 640 | return self.__wrapped__(*args, **kwargs) |
662 | 641 | os.makedirs(DEFAULT_CACHE_LOCATION, exist_ok=True) |
663 | | - return self.__backend__.get_cache_or_call( |
664 | | - func=self.__wrapped__, |
665 | | - args=args, |
666 | | - kwargs=kwargs, |
667 | | - lifespan=self.__duration__, |
668 | | - ) |
| 642 | + return self.__backend__.get_cache_or_call(func=self.__wrapped__, args=args, kwargs=kwargs, lifespan=self.__duration__) |
669 | 643 | ``` |
670 | 644 | """ |
671 | 645 | assert read_write_context.strip() == expected_read_write_context.strip() |
@@ -749,10 +723,12 @@ def __repr__(self): |
749 | 723 | expected_hashing_context = f""" |
750 | 724 | ```python:{file_path.relative_to(opt.args.project_root)} |
751 | 725 | class MyClass: |
| 726 | +
|
752 | 727 | def target_method(self): |
753 | 728 | y = HelperClass().helper_method() |
754 | 729 |
|
755 | 730 | class HelperClass: |
| 731 | +
|
756 | 732 | def helper_method(self): |
757 | 733 | return self.x |
758 | 734 | ``` |
@@ -843,10 +819,12 @@ def __repr__(self): |
843 | 819 | expected_hashing_context = f""" |
844 | 820 | ```python:{file_path.relative_to(opt.args.project_root)} |
845 | 821 | class MyClass: |
| 822 | +
|
846 | 823 | def target_method(self): |
847 | 824 | y = HelperClass().helper_method() |
848 | 825 |
|
849 | 826 | class HelperClass: |
| 827 | +
|
850 | 828 | def helper_method(self): |
851 | 829 | return self.x |
852 | 830 | ``` |
@@ -927,10 +905,12 @@ def helper_method(self): |
927 | 905 | expected_hashing_context = f""" |
928 | 906 | ```python:{file_path.relative_to(opt.args.project_root)} |
929 | 907 | class MyClass: |
| 908 | +
|
930 | 909 | def target_method(self): |
931 | 910 | y = HelperClass().helper_method() |
932 | 911 |
|
933 | 912 | class HelperClass: |
| 913 | +
|
934 | 914 | def helper_method(self): |
935 | 915 | return self.x |
936 | 916 | ``` |
@@ -1116,22 +1096,17 @@ class DataProcessor: |
1116 | 1096 | def process_data(self, raw_data: str) -> str: |
1117 | 1097 | return raw_data.upper() |
1118 | 1098 |
|
1119 | | - def add_prefix(self, data: str, prefix: str = "PREFIX_") -> str: |
| 1099 | + def add_prefix(self, data: str, prefix: str='PREFIX_') -> str: |
1120 | 1100 | return prefix + data |
1121 | 1101 | ``` |
1122 | 1102 | ```python:{path_to_file.relative_to(project_root)} |
1123 | 1103 | def fetch_and_process_data(): |
1124 | | - # Use the global variable for the request |
1125 | 1104 | response = requests.get(API_URL) |
1126 | 1105 | response.raise_for_status() |
1127 | | -
|
1128 | 1106 | raw_data = response.text |
1129 | | -
|
1130 | | - # Use code from another file (utils.py) |
1131 | 1107 | processor = DataProcessor() |
1132 | 1108 | processed = processor.process_data(raw_data) |
1133 | 1109 | processed = processor.add_prefix(processed) |
1134 | | -
|
1135 | 1110 | return processed |
1136 | 1111 | ``` |
1137 | 1112 | """ |
@@ -1225,16 +1200,11 @@ def transform_data(self, data: str) -> str: |
1225 | 1200 | ``` |
1226 | 1201 | ```python:{path_to_file.relative_to(project_root)} |
1227 | 1202 | def fetch_and_transform_data(): |
1228 | | - # Use the global variable for the request |
1229 | 1203 | response = requests.get(API_URL) |
1230 | | -
|
1231 | 1204 | raw_data = response.text |
1232 | | -
|
1233 | | - # Use code from another file (utils.py) |
1234 | 1205 | processor = DataProcessor() |
1235 | 1206 | processed = processor.process_data(raw_data) |
1236 | 1207 | transformed = processor.transform_data(processed) |
1237 | | -
|
1238 | 1208 | return transformed |
1239 | 1209 | ``` |
1240 | 1210 | """ |
@@ -1450,9 +1420,8 @@ def transform_data_all_same_file(self, data): |
1450 | 1420 | new_data = update_data(data) |
1451 | 1421 | return self.transform_using_own_method(new_data) |
1452 | 1422 |
|
1453 | | -
|
1454 | 1423 | def update_data(data): |
1455 | | - return data + " updated" |
| 1424 | + return data + ' updated' |
1456 | 1425 | ``` |
1457 | 1426 | """ |
1458 | 1427 |
|
@@ -1591,6 +1560,7 @@ def outside_method(): |
1591 | 1560 | expected_hashing_context = f""" |
1592 | 1561 | ```python:{file_path.relative_to(opt.args.project_root)} |
1593 | 1562 | class MyClass: |
| 1563 | +
|
1594 | 1564 | def target_method(self): |
1595 | 1565 | return self.x + self.y |
1596 | 1566 | ``` |
@@ -1640,16 +1610,11 @@ def transform_data(self, data: str) -> str: |
1640 | 1610 | expected_hashing_context = """ |
1641 | 1611 | ```python:main.py |
1642 | 1612 | def fetch_and_transform_data(): |
1643 | | - # Use the global variable for the request |
1644 | 1613 | response = requests.get(API_URL) |
1645 | | -
|
1646 | 1614 | raw_data = response.text |
1647 | | -
|
1648 | | - # Use code from another file (utils.py) |
1649 | 1615 | processor = DataProcessor() |
1650 | 1616 | processed = processor.process_data(raw_data) |
1651 | 1617 | transformed = processor.transform_data(processed) |
1652 | | -
|
1653 | 1618 | return transformed |
1654 | 1619 | ``` |
1655 | 1620 | ```python:import_test.py |
@@ -1915,9 +1880,9 @@ def subtract(self, a, b): |
1915 | 1880 | return a - b |
1916 | 1881 |
|
1917 | 1882 | def calculate(self, operation, x, y): |
1918 | | - if operation == "add": |
| 1883 | + if operation == 'add': |
1919 | 1884 | return self.add(x, y) |
1920 | | - elif operation == "subtract": |
| 1885 | + elif operation == 'subtract': |
1921 | 1886 | return self.subtract(x, y) |
1922 | 1887 | else: |
1923 | 1888 | return None |
|
0 commit comments