typeddjango / pytest-mypy-plugins

pytest plugin for testing mypy types, stubs, and plugins
https://pypi.org/project/pytest-mypy-plugins/
MIT License
100 stars 26 forks source link

Validate `pytest-mypy-plugins` input file schema #127

Closed stdedos closed 1 year ago

stdedos commented 1 year ago

Create a schema.yaml to:

Use said schema to meta-test all test files for conformance.

Additionally:

This fixes the real issue behind https://github.com/typeddjango/pytest-mypy-plugins/pull/124: The problem was not that mypy_config MUST HAVE {{ when parametrized was set; It was passing a list (of dicts) - which that was not templatable.

Signed-off-by: Stavros Ntentos 133706+stdedos@users.noreply.github.com

stdedos commented 1 year ago

More details about the changes:

The "main" schema is written in schema.yaml. The schema.json and schema.md are supportive files:

The files can be generated, from the repo root:

(
    set -e
    cd pytest_mypy_plugins
    yq -o=json schema.yaml >| schema.json
    jsonschema2md schema.json schema.md --examples-as-yaml
)

I haven't seen any kind of "repo/release automation" setup, so I have avoided adding those commands. Also, two external tools are required for this, so ... some extra thinking might be needed. I'd recommend pre-commit and custom hooks - but, up to you.

After that, you can do this:

image

... or, if you'd like more ✨ magic ✨ , then you can deploy it to SchemaStore (https://github.com/SchemaStore/schemastore/blob/2991ef67756c3bacfaa1bcb490c08922c13ca3f1/src/api/json/catalog.json#L1835-L1840) - but you'd have to "force-rename" the yml files to be picked-up automatically (e.g.,test_*.mypy.yaml?)

stdedos commented 1 year ago

... the only thing that bothers me is that we have 3 different schema files :)

Well ... we really have one. schema.yaml.

Can we just use schema.json for now?

Well ... yes 😢 But it will make me SO MUCH MORE SAD writing it 😭

Or yaml one?

The yaml file "cannot be used" with anything else. It's called JSONSchema 😉

sobolevn commented 1 year ago

I think that yaml is a superset of json. So, can't we just load yaml file and convert it to json in python?

This way we can at least try to have a single file for IDE-schema and validation.

stdedos commented 1 year ago

I think that yaml is a superset of json. So, can't we just load yaml file and convert it to json in python?

You can do that for https://github.com/typeddjango/pytest-mypy-plugins' validation. "IDE schema.json readers" fetch a JSON-file.

I could've, instead, avoided the load here https://github.com/typeddjango/pytest-mypy-plugins/pull/127/files#diff-f3f061be87eac60afb6957b8b8ea08b948ca1a2b12b7584f99c7611adba6232aR33 and just give "directly" the JSON file.

But then you'd have "two dependencies": yaml being the nice prototype (which you are not shipping anywhere), and the json file (which you need to remember to "change it" every time you do changes on yaml file - which, right now, they are not formally verified to match)

sobolevn commented 1 year ago

@stdedos yes, I think that this is good thing to start with: just one json file :) Later we can add more if needed.

stdedos commented 1 year ago

Ok 🤷

sobolevn commented 1 year ago

GitHub does not understand this for some reason :)

stdedos commented 1 year ago

If you start the

if you'd like more ✨ magic ✨ , then you can deploy it to SchemaStore (SchemaStore/schemastore@2991ef6/src/api/json/catalog.json#L1835-L1840) - but you'd have to "force-rename" the yml files to be picked-up automatically (e.g.,test_*.mypy.yaml?)

process, lemme know (and/or add it on your README.md)

sobolevn commented 1 year ago

I am not sure that I understand what that is :)

stdedos commented 1 year ago

You can ask https://github.com/SchemaStore/schemastore/ to include your schema in their database, so that editors pick it up automagically.

However, that means you'd have to suggest to people to name their files "more specifically", so that a glob pattern configures it automatically. They can always manually type the comment above, if they don't want to rename their tests.