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

Add Self in xarray.core.types #6923

Merged
merged 1 commit into from
Aug 22, 2022
Merged

Conversation

Illviljan
Copy link
Contributor

@Illviljan Illviljan commented Aug 16, 2022

Adds typing_extensions.Self wrapped in some safety checks.

Wont really become useful until python/mypy#11871 is fixed. But it can be used with pyright at least.

@Illviljan Illviljan changed the title Add type Self Add Self in xarray.core.types Aug 16, 2022
@Illviljan
Copy link
Contributor Author

Illviljan commented Aug 16, 2022

The https://peps.python.org/pep-0673/ has a few clear use cases. Here's a simple example with this PR uncommented:

if TYPE_CHECKING:
    from xarray.core.types import Self


class Foo:
    def returns_self(self) -> Self:
        return 1
!pyright G:\Program\Dropbox\Python\mypy_self_type.py
No configuration file found.stubPath G:\Program\Dropbox\Python\typings is not a valid directory.

No pyproject.toml file found.
Assuming Python platform Windows
Searching for source files
Found 1 source file
pyright 1.1.266
G:\Program\Dropbox\Python\mypy_self_type.py
  G:\Program\Dropbox\Python\mypy_self_type.py:45:35 - error: "Self" is not exported from module "xarray.core.types"
  Â Â Import from "typing_extensions" instead (reportPrivateImportUsage)
  G:\Program\Dropbox\Python\mypy_self_type.py:50:16 - error: Expression of type "Literal[1]" cannot be assigned to return type "Self@Foo"
  Â Â Type "int" cannot be assigned to type "Foo"
  Â Â Â Â "int" is incompatible with "Foo" (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations 
Completed in 0.778sec

Wasn't that many obvious examples where this could be used now that I started to look for it... But here's a few ones where it could be used.

) -> Index:
return self

def __enter__(self):
return self

return self

@Illviljan Illviljan added the plan to merge Final call for comments label Aug 20, 2022
@Illviljan Illviljan merged commit 790a444 into pydata:main Aug 22, 2022
@Illviljan Illviljan mentioned this pull request Sep 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments topic-typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant