Overwrites the DocTestParser from doctest to properly parse the codeblocks that are formatted with black. This means that there are no extra lines at the end of our snippets. The `# doctest: +IGNORE_RESULT` marker is also added anywhere a `load_dataset` call is made as a print would oth
| 2918 | |
| 2919 | |
| 2920 | class HfDocTestParser(doctest.DocTestParser): |
| 2921 | """ |
| 2922 | Overwrites the DocTestParser from doctest to properly parse the codeblocks that are formatted with black. This |
| 2923 | means that there are no extra lines at the end of our snippets. The `# doctest: +IGNORE_RESULT` marker is also |
| 2924 | added anywhere a `load_dataset` call is made as a print would otherwise fail the corresponding line. |
| 2925 | |
| 2926 | Tests involving cuda are skipped base on a naive pattern that should be updated if it is not enough. |
| 2927 | """ |
| 2928 | |
| 2929 | # This regular expression is used to find doctest examples in a |
| 2930 | # string. It defines three groups: `source` is the source code |
| 2931 | # (including leading indentation and prompts); `indent` is the |
| 2932 | # indentation of the first (PS1) line of the source code; and |
| 2933 | # `want` is the expected output (including leading indentation). |
| 2934 | # fmt: off |
| 2935 | _EXAMPLE_RE = re.compile(r''' |
| 2936 | # Source consists of a PS1 line followed by zero or more PS2 lines. |
| 2937 | (?P<source> |
| 2938 | (?:^(?P<indent> [ ]*) >>> .*) # PS1 line |
| 2939 | (?:\n [ ]* \.\.\. .*)*) # PS2 lines |
| 2940 | \n? |
| 2941 | # Want consists of any non-blank lines that do not start with PS1. |
| 2942 | (?P<want> (?:(?![ ]*$) # Not a blank line |
| 2943 | (?![ ]*>>>) # Not a line starting with PS1 |
| 2944 | # !!!!!!!!!!! HF Specific !!!!!!!!!!! |
| 2945 | (?:(?!```).)* # Match any character except '`' until a '```' is found (this is specific to HF because black removes the last line) |
| 2946 | # !!!!!!!!!!! HF Specific !!!!!!!!!!! |
| 2947 | (?:\n|$) # Match a new line or end of string |
| 2948 | )*) |
| 2949 | ''', re.MULTILINE | re.VERBOSE |
| 2950 | ) |
| 2951 | # fmt: on |
| 2952 | |
| 2953 | # !!!!!!!!!!! HF Specific !!!!!!!!!!! |
| 2954 | skip_cuda_tests: bool = os.environ.get("SKIP_CUDA_DOCTEST", "0") == "1" |
| 2955 | # !!!!!!!!!!! HF Specific !!!!!!!!!!! |
| 2956 | |
| 2957 | def parse(self, string, name="<string>"): |
| 2958 | """ |
| 2959 | Overwrites the `parse` method to incorporate a skip for CUDA tests, and remove logs and dataset prints before |
| 2960 | calling `super().parse` |
| 2961 | """ |
| 2962 | string = preprocess_string(string, self.skip_cuda_tests) |
| 2963 | return super().parse(string, name) |
| 2964 | |
| 2965 | |
| 2966 | class HfDoctestModule(Module): |