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

Adds covariant true to key type #244

Merged
merged 4 commits into from Jul 20, 2023

Conversation

spacether
Copy link
Contributor

Adds covariant true to key
This will allow this class to behave the same way that other immutable containers do.
If approved, this will close out this issue: #243
One should probably make this a major release as adding covariant probably makes this a breaking change. Typing experts please weigh in if you see this.

@spacether spacether changed the title Adds covariant true to key Adds covariant true to key type Jul 19, 2023
@codecov
Copy link

codecov bot commented Jul 19, 2023

Codecov Report

Merging #244 (4141169) into master (e2d2aaa) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master      #244   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           49        49           
=========================================
  Hits            49        49           
Flag Coverage Δ
3.10 100.00% <100.00%> (ø)
3.11 100.00% <100.00%> (ø)
3.12.0-beta.3 100.00% <100.00%> (ø)
3.7 100.00% <100.00%> (ø)
3.8 100.00% <100.00%> (ø)
3.9 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
immutabledict/__init__.py 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@sterliakov
Copy link

Changing variance will not be a breaking change - the worst effect it may have is breaking someone mypy pipeline with --warn-unused-ignore enabled. It does not affect runtime.

However, removing or changing .copy method will certainly be a breaking change, possible only with major version release in semver semantics.

@@ -4,6 +4,10 @@


class TestImmutableDict:
def test_covariance(self):
assert immutabledict.__parameters__[0].__covariant__ is False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, testing typing behaviour at runtime is rarely useful. Such things are usually tested by interaction and not by implementation: you can add a code file like

foo: immutabledict[str, str]
bar: immutabledict[str, str | int] = foo

and run mypy on it (considering other usage scenarios as well, of course, and using .copy there with different scenarios if you choose to keep/update it instead of removing to make sure it works as intended). Alternatively, have such lines somewhere in tests and run type checking on the test suite (some projects do this and live happily, though maintaining correct typing in tests looks like unnecessary overhead to me personally).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If immutabledict itself is type-checked in CI, you could adopt this approach where you add a file (excluded from the dist) where "try out" the library.

It's both easier to understand and more "direct" than asserting on __parameters__[0].__covariant__ :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that that would be a nicer way to test this, CI does not currently have any type checking and
my current test ensures that the feature is working

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I'd just avoid testing this rather than test typing internals. It also doesn't prove the end result.

Copy link
Contributor Author

@spacether spacether Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You and I disagree here

  • the feature that this is adding is making the key parameter covariant Removing the test would remove this future failure signal and could break future type hints.
  • the test verifies that that is the case and that that typevar is used for the key. If a developer changes this later the test will fail.
  • mypy and other type checkers implement covariance so while it would be nice to check it, a check that mypy implements covariance using immutabledict is not strictly required

If others want to add a mypy run + test to this PR or in a future PR that is fine by me

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry for the tests.

I think there are some other unrelated issues for the typing of the ordered immutabledict, I'm gonna work on it and use the opportunity to add better ci checks for the typing.

@corenting
Copy link
Owner

Thanks again for the PR @spacether !
I'm gonna merge it, there are some little updates on the README / CI I wanted to do unrelated to this , and then I'm gonna release 3.0.

@corenting corenting merged commit 33beaef into corenting:master Jul 20, 2023
10 checks passed
@spacether
Copy link
Contributor Author

You're welcome. Thanks for making the package and for the review!

@corenting corenting mentioned this pull request Jul 21, 2023
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

Successfully merging this pull request may close these issues.

None yet

4 participants