| 178 | return [line.source for line in lines] |
| 179 | |
| 180 | def validate_pep8(self): |
| 181 | if not self.examples: |
| 182 | return |
| 183 | |
| 184 | # F401 is needed to not generate flake8 errors in examples |
| 185 | # that do not user numpy or pandas |
| 186 | content = "".join( |
| 187 | ( |
| 188 | "import numpy as np # noqa: F401\n", |
| 189 | "import pandas as pd # noqa: F401\n", |
| 190 | *self.examples_source_code, |
| 191 | ) |
| 192 | ) |
| 193 | |
| 194 | error_messages = [] |
| 195 | |
| 196 | file = tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) |
| 197 | try: |
| 198 | file.write(content) |
| 199 | file.flush() |
| 200 | cmd = [ |
| 201 | sys.executable, |
| 202 | "-m", |
| 203 | "flake8", |
| 204 | "--format=%(row)d\t%(col)d\t%(code)s\t%(text)s", |
| 205 | "--max-line-length=88", |
| 206 | "--ignore=E203,E3,W503,W504,E402,E731,E128,E124,E704", |
| 207 | file.name, |
| 208 | ] |
| 209 | response = subprocess.run(cmd, capture_output=True, check=False, text=True) |
| 210 | for output in ("stdout", "stderr"): |
| 211 | out = getattr(response, output) |
| 212 | out = out.replace(file.name, "") |
| 213 | messages = out.strip("\n").splitlines() |
| 214 | if messages: |
| 215 | error_messages.extend(messages) |
| 216 | finally: |
| 217 | file.close() |
| 218 | os.unlink(file.name) |
| 219 | |
| 220 | for error_message in error_messages: |
| 221 | line_number, col_number, error_code, message = error_message.split( |
| 222 | "\t", maxsplit=3 |
| 223 | ) |
| 224 | # Note: we subtract 2 from the line number because |
| 225 | # 'import numpy as np\nimport pandas as pd\n' |
| 226 | # is prepended to the docstrings. |
| 227 | yield error_code, message, int(line_number) - 2, int(col_number) |
| 228 | |
| 229 | def non_hyphenated_array_like(self): |
| 230 | return "array_like" in self.raw_doc |