Skip to content

Commit

Permalink
Skip templating mypy_config if no placeholders exist (#124)
Browse files Browse the repository at this point in the history
The solution to `if-return` on the `render_template` was selected,
as the shortest one (vs modifying the `pytest_mypy_plugins/collect.py` file)

Signed-off-by: Stavros Ntentos <133706+stdedos@users.noreply.github.com>
  • Loading branch information
stdedos committed Sep 4, 2023
1 parent 5542939 commit 64b3a49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pytest_mypy_plugins/tests/test-parametrized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
main:2: note: Revealed type is "{{ rt }}"
main:4: error: Unsupported operand types for / ("str" and "int") [operator]
- case: parametrized_can_skip_mypy_config_section
parametrized:
- val: False
- val: True
mypy_config: |
hide_error_codes = True
main: |
a = {{ val }}
a.lower() # E: "bool" has no attribute "lower"
- case: with_mypy_config
parametrized:
Expand Down
3 changes: 3 additions & 0 deletions pytest_mypy_plugins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ def extract_output_matchers_from_out(out: str, params: Mapping[str, Any], regex:


def render_template(template: str, data: Mapping[str, Any]) -> str:
if jinja2.defaults.VARIABLE_START_STRING not in template:
return template

t: jinja2.environment.Template = _rendering_env.from_string(template)
return t.render({k: v if v is not None else "None" for k, v in data.items()})

Expand Down

0 comments on commit 64b3a49

Please sign in to comment.