• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pyta-uoft / pyta / 15304532542

28 May 2025 03:41PM UTC coverage: 93.36% (-0.1%) from 93.487%
15304532542

Pull #1183

github

web-flow
Merge b27582fc1 into fc1c64f4d
Pull Request #1183: Loaded python-ta reporter modules dynamically

19 of 24 new or added lines in 1 file covered. (79.17%)

1 existing line in 1 file now uncovered.

3459 of 3705 relevant lines covered (93.36%)

17.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.89
/python_ta/check/helpers.py
1
"""Helper functions for PythonTA's checking and reporting processes.
2
These functions are designed to support the main checking workflow by
3
modularizing core operations like file validation, linting, and result uploads.
4
"""
5

6
import importlib.util
20✔
7
import logging
20✔
8
import os
20✔
9
import re
20✔
10
import sys
20✔
11
import tokenize
20✔
12
from typing import IO, Any, AnyStr, Generator, Literal, Optional, Union
20✔
13

14
import pylint.config
20✔
15
import pylint.lint
20✔
16
import pylint.utils
20✔
17
from astroid import MANAGER, modutils
20✔
18
from pylint.exceptions import InvalidReporterError, UnknownMessageError
20✔
19
from pylint.lint import PyLinter
20✔
20
from pylint.lint.pylinter import _load_reporter_by_class
20✔
21
from pylint.reporters import BaseReporter, MultiReporter
20✔
22
from pylint.typing import Options
20✔
23
from pylint.utils.pragma_parser import OPTION_PO
20✔
24

25
from python_ta import __version__
20✔
26

27
from ..config import (
20✔
28
    find_local_config,
29
    load_config,
30
    load_messages_config,
31
    override_config,
32
)
33
from ..patches import patch_all
20✔
34
from ..upload import upload_to_server
20✔
35
from ..util.autoformat import run_autoformat
20✔
36

37
# Flag to determine if we've previously patched pylint
38
PYLINT_PATCHED = False
20✔
39

40

41
class PytaPyLinter(PyLinter):
20✔
42
    """Extenstion PyLinter that supports dynamic loading of pyta-* reporters."""
43

44
    def __init__(self, options: Options = ()):
20✔
45
        super().__init__(options=options)
20✔
46
        self.external_output_format = None
20✔
47

48
    def _load_reporter_by_name(self, reporter_name: str) -> BaseReporter:
20✔
49
        """Override the default to only load 'pyta-*' reporters"""
50
        name = self.external_output_format if self.external_output_format else reporter_name.lower()
20✔
51

52
        if "pyta-" in name:
20✔
53
            self.load_plugin_modules([self._get_reporter_module_path(name)])
20✔
54

55
        if name in self._reporters:
20✔
56
            return self._reporters[name]()
20✔
57

NEW
58
        try:
×
NEW
59
            reporter_class = _load_reporter_by_class(reporter_name)
×
NEW
60
        except (ImportError, AttributeError, AssertionError) as e:
×
NEW
61
            raise InvalidReporterError(name) from e
×
62

NEW
63
        return reporter_class()
×
64

65
    def _get_reporter_module_path(self, reporter_name: str) -> str:
20✔
66
        """Return the module path for a given PyTA reporter name."""
67
        reporter_map = {
20✔
68
            "pyta-html": "python_ta.reporters.html_reporter",
69
            "pyta-plain": "python_ta.reporters.plain_reporter",
70
            "pyta-color": "python_ta.reporters.color_reporter",
71
            "pyta-json": "python_ta.reporters.json_reporter",
72
        }
73

74
        return reporter_map.get(reporter_name, "python_ta.reporters.html_reporter")
20✔
75

76

77
def setup_linter(
20✔
78
    local_config: Union[dict[str, Any], str],
79
    load_default_config: bool,
80
    output: Optional[Union[str, IO]],
81
) -> tuple[PyLinter, Union[BaseReporter, MultiReporter]]:
82
    """Set up the linter and reporter for the check."""
83
    linter = reset_linter(config=local_config, load_default_config=load_default_config)
20✔
84
    current_reporter = linter.reporter
20✔
85
    current_reporter.set_output(output)
20✔
86
    messages_config_path = linter.config.messages_config_path
20✔
87

88
    global PYLINT_PATCHED
89
    if not PYLINT_PATCHED:
20✔
90
        patch_all(linter.config.z3)
20✔
91
        PYLINT_PATCHED = True
20✔
92
    return linter, current_reporter
20✔
93

94

95
def check_file(
20✔
96
    file_py: AnyStr,
97
    local_config: Union[dict[str, Any], str],
98
    load_default_config: bool,
99
    autoformat: Optional[bool],
100
    is_any_file_checked: bool,
101
    current_reporter: Union[BaseReporter, MultiReporter],
102
    f_paths: list,
103
) -> tuple[bool, PyLinter]:
104
    """Perform linting on a single Python file using the provided linter and configuration"""
105
    # Load config file in user location. Construct new linter each
106
    # time, so config options don't bleed to unintended files.
107
    # Reuse the same reporter each time to accumulate the results across different files.
108
    linter = reset_linter(
20✔
109
        config=local_config,
110
        file_linted=file_py,
111
        load_default_config=load_default_config,
112
    )
113

114
    if autoformat:
20✔
115
        run_autoformat(file_py, linter.config.autoformat_options, linter.config.max_line_length)
20✔
116

117
    if not is_any_file_checked:
20✔
118
        prev_output = current_reporter.out
20✔
119
        prev_should_close_out = current_reporter.should_close_out
20✔
120
        current_reporter = linter.reporter
20✔
121
        current_reporter.out = prev_output
20✔
122
        current_reporter.should_close_out = not linter.config.watch and prev_should_close_out
20✔
123

124
        # At this point, the only possible errors are those from parsing the config file
125
        # so print them, if there are any.
126
        if current_reporter.messages:
20✔
127
            current_reporter.print_messages()
20✔
128
    else:
129
        linter.set_reporter(current_reporter)
20✔
130

131
    # The current file was checked so update the flag
132
    is_any_file_checked = True
20✔
133

134
    module_name = os.path.splitext(os.path.basename(file_py))[0]
20✔
135
    if module_name in MANAGER.astroid_cache:  # Remove module from astroid cache
20✔
136
        del MANAGER.astroid_cache[module_name]
20✔
137
    linter.check([file_py])  # Lint !
20✔
138
    if linter.config.pyta_file_permission:
20✔
139
        f_paths.append(file_py)  # Appending paths for upload
×
140
    logging.debug(
20✔
141
        "File: {} was checked using the configuration file: {}".format(file_py, linter.config_file)
142
    )
143
    logging.debug(
20✔
144
        "File: {} was checked using the messages-config file: {}".format(
145
            file_py, linter.config.messages_config_path
146
        )
147
    )
148
    return is_any_file_checked, linter
20✔
149

150

151
def upload_linter_results(
20✔
152
    linter: PyLinter,
153
    current_reporter: Union[BaseReporter, MultiReporter],
154
    f_paths: list,
155
    local_config: Union[dict[str, Any], str],
156
) -> None:
157
    """Upload linter results and configuration data to the specified server if permissions allow."""
158
    config = {}  # Configuration settings for data submission
20✔
159
    errs = []  # Errors caught in files for data submission
20✔
160
    if linter.config.pyta_error_permission:
20✔
161
        errs = list(current_reporter.messages.values())
×
162
    if f_paths != [] or errs != []:  # Only call upload_to_server() if there's something to upload
20✔
163
        # Checks if default configuration was used without changing options through the local_config argument
164
        if linter.config_file[-19:-10] != "python_ta" or local_config != "":
×
165
            config = linter.config.__dict__
×
166
        upload_to_server(
×
167
            errors=errs,
168
            paths=f_paths,
169
            config=config,
170
            url=linter.config.pyta_server_address,
171
            version=__version__,
172
        )
173

174

175
def reset_linter(
20✔
176
    config: Optional[Union[dict, str]] = None,
177
    file_linted: Optional[AnyStr] = None,
178
    load_default_config: bool = True,
179
) -> PyLinter:
180
    """Construct a new linter. Register config and checker plugins.
181

182
    To determine which configuration to use:
183
    - If the option is enabled, load the default PythonTA config file,
184
    - If the config argument is a string, use the config found at that location,
185
    - Otherwise,
186
        - Try to use the config file at directory of the file being linted,
187
        - If the config argument is a dictionary, apply those options afterward.
188
    Do not re-use a linter object. Returns a new linter.
189
    """
190

191
    # Tuple of custom options. Note: 'type' must map to a value equal a key in the pylint/config/option.py `VALIDATORS` dict.
192
    new_checker_options = (
20✔
193
        (
194
            "server-port",
195
            {
196
                "default": 0,
197
                "type": "int",
198
                "metavar": "<port>",
199
                "help": "Port number for the HTML report server",
200
            },
201
        ),
202
        (
203
            "watch",
204
            {
205
                "default": False,
206
                "type": "yn",
207
                "metavar": "<yn>",
208
                "help": "Run the HTML report server in persistent mode",
209
            },
210
        ),
211
        (
212
            "pyta-number-of-messages",
213
            {
214
                "default": 0,  # If the value is 0, all messages are displayed.
215
                "type": "int",
216
                "metavar": "<number_messages>",
217
                "help": "The maximum number of occurrences of each check to report.",
218
            },
219
        ),
220
        (
221
            "pyta-template-file",
222
            {
223
                "default": "",
224
                "type": "string",
225
                "metavar": "<pyta_reporter>",
226
                "help": "HTML template file for the HTMLReporter.",
227
            },
228
        ),
229
        (
230
            "pyta-error-permission",
231
            {
232
                "default": False,
233
                "type": "yn",
234
                "metavar": "<yn>",
235
                "help": "Permission to anonymously submit errors",
236
            },
237
        ),
238
        (
239
            "pyta-file-permission",
240
            {
241
                "default": False,
242
                "type": "yn",
243
                "metavar": "<yn>",
244
                "help": "Permission to anonymously submit files and errors",
245
            },
246
        ),
247
        (
248
            "pyta-server-address",
249
            {
250
                "default": "http://127.0.0.1:5000",
251
                "type": "string",
252
                "metavar": "<server-url>",
253
                "help": "Server address to submit anonymous data",
254
            },
255
        ),
256
        (
257
            "messages-config-path",
258
            {
259
                "default": os.path.join(
260
                    os.path.dirname(os.path.dirname(__file__)), "config", "messages_config.toml"
261
                ),
262
                "type": "string",
263
                "metavar": "<messages_config>",
264
                "help": "Path to patch config toml file.",
265
            },
266
        ),
267
        (
268
            "allow-pylint-comments",
269
            {
270
                "default": False,
271
                "type": "yn",
272
                "metavar": "<yn>",
273
                "help": "Allows or disallows 'pylint:' comments",
274
            },
275
        ),
276
        (
277
            "use-pyta-error-messages",
278
            {
279
                "default": True,
280
                "type": "yn",
281
                "metavar": "<yn>",
282
                "help": "Overwrite the default pylint error messages with PythonTA's messages",
283
            },
284
        ),
285
        (
286
            "autoformat-options",
287
            {
288
                "default": ["skip-string-normalization"],
289
                "type": "csv",
290
                "metavar": "<autoformatter options>",
291
                "help": "List of command-line arguments for black",
292
            },
293
        ),
294
    )
295

296
    parent_dir_path = os.path.dirname(os.path.dirname(__file__))
20✔
297
    custom_checkers = [
20✔
298
        ("python_ta.checkers." + os.path.splitext(f)[0])
299
        for f in os.listdir(os.path.join(parent_dir_path, "checkers"))
300
        if f != "__init__.py" and os.path.splitext(f)[1] == ".py"
301
    ]
302

303
    # Register new options to a checker here to allow references to
304
    # options in `.pylintrc` config file.
305
    # Options stored in linter: `linter._all_options`, `linter._external_opts`
306
    linter = PytaPyLinter(options=new_checker_options)
20✔
307
    linter.load_default_plugins()  # Load checkers, reporters
20✔
308
    linter.load_plugin_modules(custom_checkers)
20✔
309
    linter.load_plugin_modules(["python_ta.transforms.setendings"])
20✔
310

311
    if isinstance(config, dict):
20✔
312
        linter.external_output_format = config.get("output-format", None)
20✔
313

314
    default_config_path = find_local_config(os.path.dirname(os.path.dirname(__file__)))
20✔
315
    set_config = load_config
20✔
316

317
    if load_default_config:
20✔
318
        load_config(linter, default_config_path)
20✔
319
        # If we do specify to load the default config, we just need to override the options later.
320
        set_config = override_config
20✔
321

322
    if isinstance(config, str) and config != "":
20✔
323
        set_config(linter, config)
20✔
324
    else:
325
        # If available, use config file at directory of the file being linted.
326
        pylintrc_location = None
20✔
327
        if file_linted:
20✔
328
            pylintrc_location = find_local_config(file_linted)
20✔
329

330
        # Load or override the options if there is a config file in the current directory.
331
        if pylintrc_location:
20✔
332
            set_config(linter, pylintrc_location)
×
333

334
        # Override part of the default config, with a dict of config options.
335
        # Note: these configs are overridden by config file in user's codebase
336
        # location.
337
        if isinstance(config, dict):
20✔
338
            for key in config:
20✔
339
                linter.set_option(key, config[key])
20✔
340

341
        # Override error messages
342
    messages_config_path = linter.config.messages_config_path
20✔
343
    messages_config_default_path = linter._option_dicts["messages-config-path"]["default"]
20✔
344
    use_pyta_error_messages = linter.config.use_pyta_error_messages
20✔
345
    messages_config = load_messages_config(
20✔
346
        messages_config_path, messages_config_default_path, use_pyta_error_messages
347
    )
348
    for error_id, new_msg in messages_config.items():
20✔
349
        # Create new message definition object according to configured error messages
350
        try:
20✔
351
            message = linter.msgs_store.get_message_definitions(error_id)
20✔
352
        except UnknownMessageError:
20✔
353
            logging.warning(f"{error_id} is not a valid error id.")
20✔
354
            continue
20✔
355

356
        for message_definition in message:
20✔
357
            message_definition.msg = new_msg
20✔
358
            # Mutate the message definitions of the linter object
359
            linter.msgs_store.register_message(message_definition)
20✔
360

361
    return linter
20✔
362

363

364
def get_valid_files_to_check(module_name: Union[list[str], str]) -> Generator[AnyStr, None, None]:
20✔
365
    """A generator for all valid files to check."""
366
    # Allow call to check with empty args
367
    if module_name == "":
20✔
368
        m = sys.modules["__main__"]
10✔
369
        spec = importlib.util.spec_from_file_location(m.__name__, m.__file__)
10✔
370
        module_name = [spec.origin]
10✔
371
    # Enforce API to expect 1 file or directory if type is list
372
    elif isinstance(module_name, str):
20✔
373
        module_name = [module_name]
20✔
374
    # Otherwise, enforce API to expect `module_name` type as list
375
    elif not isinstance(module_name, list):
20✔
376
        logging.error(
20✔
377
            "No checks run. Input to check, `{}`, has invalid type, must be a list of strings.".format(
378
                module_name
379
            )
380
        )
381
        return
20✔
382

383
    # Filter valid files to check
384
    for item in module_name:
20✔
385
        if not isinstance(item, str):  # Issue errors for invalid types
20✔
386
            logging.error(
20✔
387
                "No check run on file `{}`, with invalid type. Must be type: str.\n".format(item)
388
            )
389
        elif os.path.isdir(item):
20✔
390
            yield item
20✔
391
        elif not os.path.exists(os.path.expanduser(item)):
20✔
392
            try:
20✔
393
                # For files with dot notation, e.g., `examples.<filename>`
394
                yield modutils.file_from_modpath(item.split("."))
20✔
395
            except ImportError:
20✔
396
                logging.error("Could not find the file called, `{}`\n".format(item))
20✔
397
        else:
398
            yield item  # Check other valid files.
20✔
399

400

401
def get_file_paths(rel_path: AnyStr) -> Generator[AnyStr, None, None]:
20✔
402
    """A generator for iterating python files within a directory.
403
    `rel_path` is a relative path to a file or directory.
404
    Returns paths to all files in a directory.
405
    """
406
    if not os.path.isdir(rel_path):
20✔
407
        yield rel_path  # Don't do anything; return the file name.
20✔
408
    else:
409
        for root, _, files in os.walk(rel_path):
20✔
410
            for filename in (f for f in files if f.endswith(".py")):
20✔
411
                yield os.path.join(root, filename)  # Format path, from root.
20✔
412

413

414
def verify_pre_check(
20✔
415
    filepath: AnyStr,
416
    allow_pylint_comments: bool,
417
    on_verify_fail: Literal["log", "raise"] = "log",
418
) -> bool:
419
    """Check student code for certain issues.
420

421
    Precondition: `filepath` variable must be a valid file path.
422

423
    - `filepath` corresponds to the file path of the file that needs to be checked.
424
    - `allow_pylint_comments` parameter indicates whether we want the user to be able to add comments
425
       beginning with pylint which can be used to locally disable checks.
426
    - `on_verify_fail` determines how to handle files that cannot be checked. In the event that a file cannot be
427
       checked, if `on_verify_fail="raise"`, then an error is raised. However, if 'on_verify_fail="log"' (default), then
428
       False is returned.
429
    """
430
    # Make sure the program doesn't crash for students.
431
    # Could use some improvement for better logging and error reporting.
432
    try:
20✔
433
        # Check for inline "pylint:" comment, which may indicate a student
434
        # trying to disable a check.
435
        if allow_pylint_comments:
20✔
436
            return True
20✔
437
        with tokenize.open(os.path.expanduser(filepath)) as f:
20✔
438
            for tok_type, content, _, _, _ in tokenize.generate_tokens(f.readline):
20✔
439
                if tok_type != tokenize.COMMENT:
20✔
440
                    continue
20✔
441
                match = OPTION_PO.search(content)
20✔
442
                if match is not None:
20✔
443
                    logging.error(
20✔
444
                        'String "pylint:" found in comment. '
445
                        + "No check run on file `{}.`\n".format(filepath)
446
                    )
447
                    return False
20✔
448
    except IndentationError as e:
20✔
449
        logging.error(
20✔
450
            "python_ta could not check your code due to an "
451
            + "indentation error at line {}.".format(e.lineno)
452
        )
453
        if on_verify_fail == "raise":
20✔
454
            raise
20✔
455
        return False
20✔
456
    except tokenize.TokenError as e:
20✔
457
        logging.error(
20✔
458
            "python_ta could not check your code due to a " + "syntax error in your file."
459
        )
460
        if on_verify_fail == "raise":
20✔
461
            raise
20✔
462
        return False
20✔
463
    except UnicodeDecodeError as e:
20✔
464
        logging.error(
20✔
465
            "python_ta could not check your code due to an "
466
            + "invalid character. Please check the following lines "
467
            "in your file and all characters that are marked with a �."
468
        )
469
        with open(os.path.expanduser(filepath), encoding="utf-8", errors="replace") as f:
20✔
470
            for i, line in enumerate(f):
20✔
471
                if "�" in line:
20✔
472
                    logging.error(f"  Line {i + 1}: {line}")
20✔
473
        if on_verify_fail == "raise":
20✔
474
            raise
20✔
475
        return False
20✔
476
    return True
20✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc