[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute

2021年6月26日 16:55:56 -0700

Found the mypy bug:
https://github.com/python/mypy/issues/4125 
On Sat, Jun 26, 2021 at 7:35 PM Daniel Walker <[email protected]> wrote:
> I wasn't looking at the type stub but cmd.py itself. It has
>
> PROMPT = '(Cmd) '
> ...
>
> class Cmd:
> prompt = PROMPT
> ...
>
> On Sat, Jun 26, 2021 at 6:04 PM Guido van Rossum <[email protected]> wrote:
>
>> On Sat, Jun 26, 2021 at 9:25 AM Daniel Walker <[email protected]>
>> wrote:
>>
>>> I was recently using the cmd module for a project where my CLI
>>> could connect to and interact with another host. I implemented prompt in
>>> such a way that it would show the IP address when connected. I.e.,
>>>
>>> class MyCmd(cmd.Cmd):
>>> ...
>>>
>>> @property
>>> def prompt(self) -> str:
>>> if self.remote_host.connected():
>>> return f'> ({self.remote_host.ip}) '
>>> else:
>>> return '> '
>>>
>>> This worked perfectly fine... until I ran mypy. mypy complained
>>> because, in cmd.Cmd, prompt is a class attribute.
>>>
>>> Looking at cmd.py, this seems like an odd design choice as all of the
>>> references to cmd are through the instance (i.e., self.prompt).
>>>
>>
>> You misread the typeshed stub. Where you see these lines in cmd.pyi
>>
>> class Cmd:
>> prompt: str
>> identchars: str
>> ruler: str
>> ...
>>
>> those are all instance attribute declarations.
>>
>> I think that you're running into a different mypy bug, which is that you
>> can't override a plain attribute with a property in a subclass.
>>
>> I think there's already a bug for that in the mypy tracker, but I can't
>> find it right now.
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>> *Pronouns: he/him **(why is my pronoun here?)*
>> <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
>>
>
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/ZJ6FN2DNDYN66RMC6UEYBQPTQ5SAPQEG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to