Skip to content

inspect: Add positions attributes that will be new in 3.11 #7688

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

Merged
merged 8 commits into from
Apr 29, 2022

Conversation

JelleZijlstra
Copy link
Member

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.

Defining __new__ looks cleaner than what I did for dis. Edit: it also happens to be what the runtime does

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

Defining __new__ looks cleaner than what I did for dis. Edit: it also happens to be what the runtime does

It does however break mypy 🙃

@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
Member Author

hm, now I get stdlib/inspect.pyi:472: error: Cannot overwrite NamedTuple attribute "__new__" [misc]. Do we have a better technique or should we just type ignore it?

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

hm, now I get stdlib/inspect.pyi:472: error: Cannot overwrite NamedTuple attribute "__new__" [misc]. Do we have a better technique or should we just type ignore it?

We could do something similar to what I did with structseq classes in other modules. But I don't think there's any need, because (unlike with structseqs), there's no attributes on these classes that are ClassVars. And mypy can infer the types fine, even if it doesn't think it can, so I say: just type: ignore it away.

https://mypy-play.net/?mypy=latest&python=3.10&gist=2bdfa6f9cdc2bdd07c5d472bf4124ea5

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 26, 2022

On second thoughts, why don't we just mimic the runtime implementation exactly? It seems to work pretty well for tokenize.TokenInfo:

class _TokenInfo(NamedTuple):
type: int
string: str
start: _Position
end: _Position
line: str
class TokenInfo(_TokenInfo):
@property
def exact_type(self) -> int: ...

and it would mean we could override __new__ without a # type: ignore. It also means that we can annotate the positions attributes as settable instance variables, which they are at runtime in 3.11, even though all other attributes on Traceback and FrameInfo are read-only.

So in the 3.11 branch we would have:

if sys.version_info >= (3, 11):
    class _Traceback(NamedTuple):
        filename: str
        lineno: int
        function: str
        code_context: list[str] | None
        index: int | None  # type: ignore[assignment]

    class Traceback(_Traceback):
        positions: dis.Positions | None
        def __new__(
            cls: type[Self],
            filename: str,
            lineno: int,
            function: str,
            code_context: list[str] | None,
            index: int | None,
            *,
            positions: dis.Positions | None = ...,
        ) -> Self: ...

    class _FrameInfo(NamedTuple):
        frame: FrameType
        filename: str
        lineno: int
        function: str
        code_context: list[str] | None
        index: int | None  # type: ignore[assignment]

    class FrameInfo(_FrameInfo):
        positions: dis.Positions | None
        def __new__(
            cls: type[Self],
            frame: FrameType,
            filename: str,
            lineno: int,
            function: str,
            code_context: list[str] | None,
            index: int | None,
            *,
            positions: dis.Positions | None = ...,
        ) -> Self: ...

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@AlexWaygood AlexWaygood merged commit 145e659 into master Apr 29, 2022
@AlexWaygood AlexWaygood deleted the JelleZijlstra-patch-6 branch April 29, 2022 23:00
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.

3 participants