Skip to content

stubtest: emit error if a stub defines a public type alias that doesn't exist at runtime - #12608

Merged
hauntsaninja merged 5 commits into
python:masterfrom
AlexWaygood:stubtest-typealiases
Apr 18, 2022
Merged

stubtest: emit error if a stub defines a public type alias that doesn't exist at runtime#12608
hauntsaninja merged 5 commits into
python:masterfrom
AlexWaygood:stubtest-typealiases

Conversation

@AlexWaygood

@AlexWaygoodAlexWaygood commented Apr 17, 2022

Copy link
Copy Markdown
Member

Description

A followup to python/typeshed#7634 (comment)

Stubtest currently declines to error if a stub defines a public type alias that doesn't exist at runtime. But it should!

These are the new error reported from running stubtest on the typeshed stdlib, with this patch applied:

error: _threading_local.localdict is not present at runtime
Stub: at line 6
Type alias for: builtins.dict
Runtime:
MISSING
error: audioop.AdpcmState is not present at runtime
Stub: at line 3
Type alias for: Tuple[builtins.int, builtins.int]
Runtime:
MISSING
error: audioop.RatecvState is not present at runtime
Stub: at line 4
Type alias for: Tuple[builtins.int, builtins.tuple[Tuple[builtins.int, builtins.int], ...]]
Runtime:
MISSING
error: itertools.Predicate is not present at runtime
Stub: at line 14
Type alias for: def (_T?) -> builtins.object
Runtime:
MISSING
error: sqlite3.dbapi2.OptimizedUnicode is not present at runtime
Stub: at line 413
Type alias for: builtins.str
Runtime:
MISSING

They all look like true positives to me (or at least should be allowlisted in typeshed, rather than being ignored by stubtest altogether).

Test Plan

Two test cases added

@AlexWaygoodAlexWaygood changed the title stubtest: emit error if a stub defines a public type alias that's missing at runtimestubtest: emit error if a stub defines a public type alias that doesn't exist at runtimeApr 17, 2022

@hauntsaninjahauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks! I think it can be easier to preserve line numbers... I took the liberty of pushing changes directly to the branch, let me know what you think!

@AlexWaygood

Copy link
Copy Markdown
MemberAuthor

I think it can be easier to preserve line numbers... I took the liberty of pushing changes directly to the branch, let me know what you think!

Nice, thank you!! That's... much simpler than my method 🤦‍♂️

Just one nit about error messages -- we currently have:

Stub: at line 4
Type alias for Tuple[builtins.int, builtins.tuple[Tuple[builtins.int, builtins.int], ...]]

I'd much prefer either this, separating the English from the code using a colon:

Stub: at line 4
Type alias for: Tuple[builtins.int, builtins.tuple[Tuple[builtins.int, builtins.int], ...]]

or this, separating the English from the code using quotation marks:

Stub: at line 4
Type alias for "Tuple[builtins.int, builtins.tuple[Tuple[builtins.int, builtins.int], ...]]"

What do you think?

Comment threadmypy/stubtest.py Outdated

@AlexWaygoodAlexWaygood left a comment

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I approve my own PR

@hauntsaninja
hauntsaninja merged commit 9e9de71 into python:masterApr 18, 2022
@AlexWaygood
AlexWaygood deleted the stubtest-typealiases branch April 18, 2022 20:27
@AlexWaygood

Copy link
Copy Markdown
MemberAuthor

Thanks @hauntsaninja :)

Sign up for freeto 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.

2 participants

@AlexWaygood@hauntsaninja