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

disallow untyped defs #289

Merged
merged 32 commits into from
May 19, 2021
Merged

Conversation

graingert
Copy link
Collaborator

@graingert graingert commented May 11, 2021

disallow untyped defs and fix runtime problems discovered by typing all defs:

  • TextStream.extra_attributes no longer crashes with an attribute error
  • await maybe_async(current_task()) no longer returns None
  • fix warning message to refer to anyio.maybe_async
  • pickle.dumps(get_current_task()) now correctly raises a TypeError (or similar on pypy) rather than pickling to None
  • task names are converted to str on the asyncio back-end

also type level errors:

  • various types, such as anyio.Lock() that were missing a constructor type now have one, see Function is missing type annotation for __init__(self) python/mypy#5943 (comment)
  • anyio._backend._asyncio.run takes a coroutine factory and returns the coroutine result - rather than any callable
  • await anyio.Event().wait() returns None at runtime, rather than bool. Updated type annotations to reflect this
  • anyio.lowlevel.RunVar().get type annotation was updated to return either the default value or the contained type

@graingert graingert force-pushed the disallow-untyped-defs branch from 6aa6949 to a14d442 Compare May 12, 2021 11:10
@graingert graingert force-pushed the disallow-untyped-defs branch from a14d442 to 010c435 Compare May 12, 2021 14:07
@graingert graingert force-pushed the disallow-untyped-defs branch from 7aa7b52 to 9b14209 Compare May 12, 2021 14:11
@graingert graingert marked this pull request as ready for review May 12, 2021 14:12
@graingert graingert changed the title disallow untyped defs WIP disallow untyped defs May 12, 2021
@uSpike
Copy link
Contributor

uSpike commented May 12, 2021

Should there be a new wrapper function for asyncio current_task() which always returns a task? This would avoid all of the casting and # type: ignore statements.

@graingert graingert force-pushed the disallow-untyped-defs branch from 3692393 to 4b76b93 Compare May 12, 2021 14:45
@graingert graingert force-pushed the disallow-untyped-defs branch from 4b76b93 to ebb72c3 Compare May 12, 2021 15:01
@graingert graingert requested a review from agronholm May 12, 2021 16:40
Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

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

Some question, some change suggestions.

src/anyio/_backends/_trio.py Outdated Show resolved Hide resolved
src/anyio/_core/_compat.py Show resolved Hide resolved
src/anyio/_core/_fileio.py Show resolved Hide resolved
src/anyio/_core/_streams.py Outdated Show resolved Hide resolved
src/anyio/_core/_typedattr.py Show resolved Hide resolved
src/anyio/abc/_sockets.py Outdated Show resolved Hide resolved
src/anyio/from_thread.py Show resolved Hide resolved
src/anyio/lowlevel.py Outdated Show resolved Hide resolved
src/anyio/streams/file.py Outdated Show resolved Hide resolved
src/anyio/to_process.py Outdated Show resolved Hide resolved
@graingert graingert requested a review from agronholm May 18, 2021 09:47
Copy link
Owner

@agronholm agronholm left a comment

Choose a reason for hiding this comment

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

Almost there! Just a suggestion regarding the type of extra_attributes, and some minor changes.

src/anyio/_backends/_trio.py Outdated Show resolved Hide resolved
Comment on lines 126 to +130
class TaskGroup(abc.TaskGroup):
def __init__(self):
def __init__(self) -> None:
self._active = False
self._nursery_manager = trio.open_nursery()
self.cancel_scope = None
self.cancel_scope = None # type: ignore[assignment]
Copy link
Owner

Choose a reason for hiding this comment

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

Since the cancel_scope member is never accessed unless the cancel scope is active, perhaps this should be declared just with its type and no default value on the class? Without setting it to None in the initializer?

Copy link
Collaborator Author

@graingert graingert May 19, 2021

Choose a reason for hiding this comment

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

I think that's pretty much equivalent to a # type: ignore[assignment], but means you're assigning to attributes outside the constructor. I think the real fix is to use a TaskGroup/TaskGroupManager pattern like trio:

class TaskGroup(abc.TaskGroup):
    def __init__(self, nursery: trio.Nursery):
        self._active = True
        self._nursery = nursery
        self._cancel_scope = CancelScope(nursery.cancel_scope)

class TaskGroupManager(abc.TaskGroupManager):
    def __init__(self):
        self._nursery_manager = trio.open_nursery()

    async def __aenter____(self):
        return TaskGroup(await self._nursery_manager.__aenter__())

    async def __aexit__(self, *args, **kwargs):
        return await self._nursery_manager.__aexit__()

src/anyio/_backends/_trio.py Outdated Show resolved Hide resolved
src/anyio/_core/_testing.py Outdated Show resolved Hide resolved
src/anyio/abc/_resources.py Outdated Show resolved Hide resolved
src/anyio/from_thread.py Outdated Show resolved Hide resolved
src/anyio/lowlevel.py Outdated Show resolved Hide resolved
src/anyio/pytest_plugin.py Show resolved Hide resolved
src/anyio/streams/file.py Outdated Show resolved Hide resolved
src/anyio/to_thread.py Outdated Show resolved Hide resolved
@graingert graingert force-pushed the disallow-untyped-defs branch from 0663752 to f3d7411 Compare May 19, 2021 10:08
@graingert graingert requested a review from agronholm May 19, 2021 10:29
@agronholm agronholm merged commit e23b44e into agronholm:master May 19, 2021
@agronholm
Copy link
Owner

Thank you for your hard work on this!

@graingert graingert deleted the disallow-untyped-defs branch May 19, 2021 13:52
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.

4 participants