-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Missed dependency when import web3: typing_extensions #1540
Comments
Thank you for pointing this out! I will open a PR shortly! |
@kclowes we have some tests in trinity that actually build the wheel, install it, and then run some tests against it. Maybe that type of thing would be useful here as it would catch this type of thing. |
That would be super useful. I'll check it out! |
Just as a thought, typing extensions should really never be required at runtime, so rather than simply adding typing_extensions as a requirement (which is ok for just a short-term solution), I would advocate for surrounding any usage of types with Python type checking is only for static type checking, so it doesn't need to (and shouldn't) be required at runtime. Taking a quick look, this is already done for some types, but not all of them i.e: Lines 98 to 107 in 0aa0766
Making sure that all typing imports are behind an |
You're right, @cheeseandcereal, that seems like a better fix. This issue has the quick fix implemented in 5.4.0 by adding |
Yup, this is most likely my fault from all the type-stuff I've been doing. So there's However, say a user is running python 3.6/3.7, then they need to have However, hiding all the extended types behind an |
That's not exactly true though. The class SupportsError(Protocol):
error: Exception If I'm not mistaking I think this is added in Python 3.8 so that class SyncProgress(NamedTuple):
starting_block: BlockNumber
current_block: BlockNumber
highest_block: BlockNumber And you will absolutely need to have this at runtime because you would use it in regular function bodies such as: def update_current_block(self, new_current_block: BlockNumber) -> SyncProgress:
return SyncProgress(self.starting_block, new_current_block, self.highest_block) |
@cburgdorf I agree that namedtuple would be an exception, but that's only because a namedtuple is a literal collections object type. A python protocol should never need to be imported or defined at runtime in order to take advantage of its typing features. ( mytypes.py from typing_extensions import Protocol
# from typing import Protocol <- Python 3.8+
class MyProto(Protocol):
def some_method(self) -> int:
... main.py from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mytypes import MyProto
class MyImplementation():
def some_method(self) -> int:
return 0
def do_the_thing(x: "MyProto") -> int:
return x.some_method()
if __name__ == "__main__":
my_obj = MyImplementation()
print(do_the_thing(my_obj) + 1) This gives us the static type checking advantage of protocols, without ever having to use them at runtime. The trick is that all of your types, including protocols need to be defined in a separate file which is only ever imported behind an |
I think it depends. Protocols even allow to implement default implementations Also, you may want to check I personally think it's a safer default to not treat |
I suppose that is true. At least in that case, |
Closing, this was fixed a long time ago in #1546. |
pip freeze
outputWhat was wrong?
When I want to import web3, I catch the following error:
I found that this dependency is used in the files:
How can it be fixed?
Add typing_extensions library into setup.py file to install_requires list (line 70-87)
The text was updated successfully, but these errors were encountered: