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

[stubtest] Verify __all__ exists in stub #18005

Merged
merged 5 commits into from
Oct 23, 2024

Conversation

srittau
Copy link
Contributor

@srittau srittau commented Oct 21, 2024

Previously it wasn't an error if runtime included an
__all__ declaration, but the stubs did not. This PR
changes this to reflect the consensus that it would
be a good idea to ensure consistency in this case.

Fixes #13300

@JelleZijlstra
Copy link
Member

Does this cause a lot of new errors in typeshed?

@srittau
Copy link
Contributor Author

srittau commented Oct 21, 2024

How can we check that? If it does, I suggest we just mass add .*\.__all__ # TODO to the stubtest allowlists, like @Avasam suggested.

@JelleZijlstra
Copy link
Member

I tried locally (by installing this PR branch and running typeshed tests), and didn't see any new errors—which actually seems a little suspicious.

@srittau
Copy link
Contributor Author

srittau commented Oct 21, 2024

I tried locally (by installing this PR branch and running typeshed tests), and didn't see any new errors—which actually seems a little suspicious.

Very. I suspect there to be a quite significant number of hits.

@JelleZijlstra
Copy link
Member

It was because of #18007; I forgot that stubtest doesn't do anything if there are mypy errors in the stubs.

With this patch:

diff --git a/pyproject.toml b/pyproject.toml
index 46014bcd2..77e5e3127 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -166,3 +166,6 @@ known-first-party = ["ts_utils", "_utils"]
 
 [tool.typeshed]
 oldest_supported_python = "3.8"
+
+[tool.mypy]
+disable_error_code = "deprecated"
diff --git a/tests/stubtest_stdlib.py b/tests/stubtest_stdlib.py
index 5134f8270..77dab3d20 100755
--- a/tests/stubtest_stdlib.py
+++ b/tests/stubtest_stdlib.py
@@ -29,6 +29,8 @@ def run_stubtest(typeshed_dir: Path) -> int:
         "--check-typeshed",
         "--custom-typeshed-dir",
         str(typeshed_dir),
+        "--mypy-config-file",
+        str(typeshed_dir / "pyproject.toml"),
         *allowlist_stubtest_arguments("stdlib"),
     ]
     if sys.version_info < (3, 10):

I get these in the stdlib on macOS and 3.12:

error: asyncio.__all__ is not present in stub
error: email.__all__ is not present in stub
error: email._policybase.__all__ is not present in stub
error: importlib.resources.abc.__all__ is not present in stub
error: os.__all__ is not present in stub
error: socket.__all__ is not present in stub
error: xml.__all__ is not present in stub
error: zipfile._path.__all__ is not present in stub
note: unused allowlist entry concurrent.futures.__all__

Didn't run it on all the third-party stubs.

@srittau
Copy link
Contributor Author

srittau commented Oct 21, 2024

Interesting. I just commented out __all__ in stubs/passlib/passlib/utils/__init__.pyi, ran stubtest on passlib and didn't get any warnings. There is __all__ at runtime.

@JelleZijlstra
Copy link
Member

Did you use stubtest_third_party.py? Looks like it always installs the mypy from our requirements.txt, so it wouldn't pick up local changes.

@srittau
Copy link
Contributor Author

srittau commented Oct 21, 2024

Yes, it seems that way. I added debug statements to the installed mypy and those are not printed either.

@JelleZijlstra
Copy link
Member

Yes, if you want to test you'll have to edit requirements.txt or otherwise hack the script. I think the stdlib results are enough for me to believe that this works as expected, so I'm happy to merge this, but I'll wait a little bit in case Shantanu/Alex/someone else seeing this has more feedback.

@Avasam
Copy link
Contributor

Avasam commented Oct 21, 2024

I do have a question: Do we want to report cases where __all__ is missing in stubs, but __all__ at runtime is redundant? (ie: re-exports all symbols anyway, so star-imports wouldn't be affected). I don't feel strongly either way, I just want to make sure the decision is explicit.

@srittau
Copy link
Contributor Author

srittau commented Oct 21, 2024

I do have a question: Do we want to report cases where __all__ is missing in stubs, but __all__ at runtime is redundant? (ie: re-exports all symbols anyway, so star-imports wouldn't be affected). I don't feel strongly either way, I just want to make sure the decision is explicit.

I would say yes. Stubs beings closer to the implementation has value in itself, instead of special casing specific cases. Also, it means that x.__all__ type checks correctly. There might be other use cases that we don't know about yet.

mypy/stubtest.py Outdated Show resolved Hide resolved
mypy/test/teststubtest.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thank you!

@hauntsaninja hauntsaninja merged commit eb0575e into python:master Oct 23, 2024
12 checks passed
@srittau srittau deleted the stubtest-all branch October 24, 2024 04:52
hannes added a commit to duckdb/duckdb that referenced this pull request Dec 30, 2024
1.14 introduced [a change](python/mypy#18005)
that our `__init__.py` doesn't, and can't comply with.

Our `__init__.py` looks like this:
```py
_exported_symbols.extend([
    "Value",
    "NullValue",
    "BooleanValue",
    "UnsignedBinaryValue",
    "UnsignedShortValue",
    "UnsignedIntegerValue",
    "UnsignedLongValue",
    "BinaryValue",
    "ShortValue",
    "IntegerValue",
    "LongValue",
    "HugeIntegerValue",
    "FloatValue",
    "DoubleValue",
    "DecimalValue",
    "StringValue",
    "UUIDValue",
    "BitValue",
    "BlobValue",
    "DateValue",
    "IntervalValue",
    "TimestampValue",
    "TimestampSecondValue",
    "TimestampMilisecondValue",
    "TimestampNanosecondValue",
    "TimestampTimeZoneValue",
    "TimeValue",
    "TimeTimeZoneValue",
])

__all__ = _exported_symbols
```

Which I'm pretty sure is non-standard, and creates a warning, but this
works for us

Mypy is not happy about this because it seems `__all__` is expected to
be a constant expression, and expected to be copied to the
`__init__.pyi` file
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.

Should stubtest verify the presence of __all__ in a stub?
4 participants