Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type-checking columns #259

Closed
mikapfl opened this issue Sep 12, 2023 · 3 comments
Closed

Type-checking columns #259

mikapfl opened this issue Sep 12, 2023 · 3 comments

Comments

@mikapfl
Copy link
Member

mikapfl commented Sep 12, 2023

Describe the bug

This:

import scmdata

def test() -> scmdata.ScmRun:
    columns = {"regions": ["here", "there"], "model": "test"}
    return scmdata.ScmRun(
        index=[1, 2, 3],
        data=np.ones((3, 2)),
        columns=columns,
    )

does not type-check, with the error:

error: Argument "columns" to "ScmRun" has incompatible type "dict[str, Sequence[str]]"; expected "Mapping[Hashable, str | int | float | Iterable[str | int | float]] | None"  [arg-type]

If you specify the columns directly in the call:


def test() -> scmdata.ScmRun:
    return scmdata.ScmRun(
        index=[1, 2, 3],
        data=np.ones((3, 2)),
        columns={"regions": ["here", "there"], "model": "test"},
    )

It type-checks no problem.

Expected behaviour

I expect this common idiom to be fine.

Additional context

I'm confused why this fails. Apparently, Hashable isn't properly supported by mypy? Something about mutability etc.

Maybe we just (in the type hints) put our foot to the ground and say that metadata column names have to be str?

Or maybe there is another solution which I'm missing.

@lewisjared
Copy link
Collaborator

Let's use str instead of hashable. It is probably good to enforce string-based metadata columns anyway. What would an integer as a column name represent???

@mikapfl
Copy link
Member Author

mikapfl commented Sep 13, 2023

What would an integer as a column name represent???

You're not thinking big enough, tuple are also hashable 😉

Let's use str instead of hashable.

To fix this, we also have to use Mappable instead of dict, otherwise I get:

thing.py:7: error: Argument 1 to "a" has incompatible type "Dict[str, str]"; expected "Dict[str, Union[str, Iterable[str]]]"  [arg-type]
thing.py:7: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
thing.py:7: note: Consider using "Mapping" instead, which is covariant in the value type
Found 1 error in 1 file (checked 1 source file)

As you said, typing support is not quite as mature in Python as it is in other languages…

@lewisjared
Copy link
Collaborator

Updated columns type hint to columns: Mapping[str, MetadataValue | Iterable[MetadataValue]] | None in #262

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants