Skip to content

request: Statically evaluating module-level hasattr checks #9042

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

Open
njsmith opened this issue Jun 24, 2020 · 3 comments
Open

request: Statically evaluating module-level hasattr checks #9042

njsmith opened this issue Jun 24, 2020 · 3 comments

Comments

@njsmith
Copy link

njsmith commented Jun 24, 2020

This is a feature request, for mypy to enhance its static evaluation for boolean expressions involving sys.platform/sys.version_info to also handle expressions like hasattr(module_object, "constant string").

background

A common idiom in the standard library is to have functions/constants/etc. that are only exposed on certain platforms. For example, select.epoll only exists on Linux and illumos, socket.fromshare only exists on Windows, and os.preadv only exists on "Linux 2.6.30 and newer, FreeBSD 6.0 and newer, OpenBSD 2.7 and newer".

Currently, the way mypy handles these cases is:

  • typeshed contains if sys.platform == ... checks that try to approximate the runtime availability. Example
  • then mypy statically evaluates these checks, so it knows which functions/constants/etc. are available

So that works about as well as anything could. The static approximations aren't always exactly correct (e.g. the epoll example linked above gives the wrong answer on illumos), but they're "good enough" and can be improved over time if they cause problems.

the problem

Say we have a library which wants to use or expose certain features in its API depending on whether or not the standard library exposes those features. For example, in Trio we define our IOManager class differently depending on whether select.epoll and select.kqueue exist, and we export a trio.socket.fromshare function iff there's a socket.fromshare.

Now we're trying to figure out how to add type annotations, and it's super awkward. So far our best attempt for the basic platform differences looks like:

if sys.platform == "win32":
    ...
elif sys.platform == "linux" or (not TYPE_CHECKING and hasattr(select, "epoll")):
    ...
# At type-checking time, assume that all platforms that aren't win32 or linux are some kind of BSD
elif TYPE_CHECKING or (not TYPE_CHECKING and hasattr(select, "kqueue")):
    ...
else:
    raise NotImplementedError

This has a few problems:

  • it's complex and awkward
  • it requires manually duplicating information that's already in typeshed
  • it requires manually propagating any typeshed changes into our project (which seems plausible, since typeshed often contains rough approximations for platform-specific stuff)
  • it requires copy/pasting this whole complex construct in multiple places around our code-base

This is a bit frustrating. It would be nice if mypy could just understand if hasattr(select, "epoll") and do the right thing. It already has all the information it needs to do that.

@bluetech
Copy link
Contributor

Not sure if this was suggested before, or how hard it will be, but a generalization would be to:

  1. Type getattr(obj, "attr"), where attr is a single str Literal, as obj.attr if attr statically exists on obj, and Any otherwise (could have a strictness flag which turns it into an attr-defined error).

  2. Type getattr(obj, "attr", default), where attr is a single str Literal, as obj.attr if attr statically exists on obj, and the type of default otherwise.

This is generally useful (I wanted it a few times). For the feature-checking use case you could do if getattr(select, 'epoll', None) is not None.

@njsmith
Copy link
Author

njsmith commented Jun 29, 2020

@bluetech I think that should be discussed in a separate issue. In particular, this isn't about how expressions are typed, but about which expressions mypy will evaluate statically when deciding to entirely skip over a block of code, and currently that set of expressions is very limited (basically just and/or/TYPE_CHECKING/sys.platform/sys.version_info).

For the feature-checking use case you could do if getattr(select, 'epoll', None) is not None.

This would require a lot of additional complexity in the skip-over-statically-evaluable-branches code, and it's not at all clear that you'd even want to skip type-checking code inside a if guarded by getattr. There's a reason my proposal is restricted to just the pattern hasattr(module_object, "literal string"), only :-)

@lephuongbg
Copy link

There is already this issue that requests the same thing: #1424

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants