kracekumar / python-typing-koans

Python typing koans to learn type-hints in Python3 using Mypy
119 stars 10 forks source link

114-medium-factory-pattern #11

Open J0 opened 3 years ago

J0 commented 3 years ago

Hey @kracekumar,

Thanks for taking the time to reply to all my past queries. I'm a little stuck on 114 and was wondering if you could provide a hint or suggestion of sorts. I'm currently using overloaded method signatures for sock_type in the following format:

@overload
def _sock_type(addr: Tuple[str,int])-> Union[TCPSocket,TCP6Socket]:...
@overload
def _sock_type(addr: Union[str, bytes])-> UnixSocket:...

def _sock_type(addr: Address):
      <implementation details>

However, this solution still uses union and unfortunately it also means that sock_type in the line below is TCP Socket, TCP6Socket or Unix Socket whic produces the type error sock_type is not callable.

sock = sock_type(address, conf, log)

Was wondering if you have any hints/suggestions/tips.

Let me know

kracekumar commented 3 years ago

Glad to see your question and trying out the exercise.

You don't need to use overload for this exercise or refactor the code. While dealing with the inherited classes, most of the time, the child classes will follow the base class signature. So try using the base class as a type rather than using Uniontype and see if it works.

Looking forward to your experience.

kracekumar commented 3 years ago

@J0 did you get it working with the hints from the previous comment?

J0 commented 3 years ago

@kracekumar thanks for checking in! I haven't had time to look at it but will give it a try again tonight and let you know how it goes :)

J0 commented 3 years ago

Done! Think this post: https://stackoverflow.com/questions/55441612/does-mypy-have-a-subclass-acceptable-return-type was particularly helpful

Thanks for the advice :)