[Python-Dev] cmd.Cmd.prompt should be an instance attribute
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).
While it's easy to fix this in my subclass by overriding the onecmd method
and setting self.prompt there, this seems kludgy ("Beautiful is better than
ugly"). More importantly, this whole arrangement obscures the fact that
prompt is a de facto attribute of the instance and not the class. It's not
the class that's displaying the prompt but the instance.
It seems more intuitive for cmd.Cmd to be implemented thus:
class Cmd:
PROMPT = '> '
def __init__(self):
...
self.prompt = self.PROMPT
___
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/PCXEIWFOJ23ZVFWXW7NGTOSMAKKGMQ4Z/
Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute
Sorry, that implementation should have been class Cmd: PROMPT = '> ' @property def prompt(self) -> str: return self.PROMPT ___ 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/Q7TSFJD4EPUXXJFT5TMBUODIVL4CLR4O/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute
A class attribute provides a default that’s the same for all instances, but does let you customize it with a simple attribute assignment. Which seems like the right thing in this case. class Cmd: > PROMPT = '> ' > > @property > def prompt(self) -> str: > return self.PROMPT This makes .prompt a read-only instance attribute. So you then wouldn’t be able to change it for a given instance. Why make it an instance attribute if all instances will have the same one? Unless you want to disallow overriding it in an instance. If the reason is to make MyPy happy, then either there’s a big in your code, or MyPy is being overly pedantic. NOTE: accessing class attributes via self is standard practice — after all methods are simply callable class attributes :-) -CHB > ___ > 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/Q7TSFJD4EPUXXJFT5TMBUODIVL4CLR4O/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython ___ 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/GAU3BJFQ2OYLQSRPIWZKH7CKFCK76L32/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute
On Sat, Jun 26, 2021 at 9:25 AM Daniel Walker 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?)*
___
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/TNDQVFAA3XLOOMRQ5RFMRMW52Y3DF2ZV/
Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute
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 wrote:
> On Sat, Jun 26, 2021 at 9:25 AM Daniel Walker 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?)*
>
>
___
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/XAENJVNK7US2BRUPOIFIUMY2N7MTHCWT/
Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute
Found the mypy bug:
https://github.com/python/mypy/issues/4125
On Sat, Jun 26, 2021 at 7:35 PM Daniel Walker 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 wrote:
>
>> On Sat, Jun 26, 2021 at 9:25 AM Daniel Walker
>> 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?)*
>>
>>
>
___
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/
[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute
Okay, then Chris Barker’s explanation applies.
On Sat, Jun 26, 2021 at 16:35 Daniel Walker 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 wrote:
>
>> On Sat, Jun 26, 2021 at 9:25 AM Daniel Walker
>> 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?)*
>>
>>
> --
--Guido (mobile)
___
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/JONOXZ5VFY3AFCOGN6UF5AUHASINRVXA/
Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: cmd.Cmd.prompt should be an instance attribute
Thank you
On Sat, Jun 26, 2021 at 8:10 PM Guido van Rossum wrote:
> Okay, then Chris Barker’s explanation applies.
>
> On Sat, Jun 26, 2021 at 16:35 Daniel Walker 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
>> wrote:
>>
>>> On Sat, Jun 26, 2021 at 9:25 AM Daniel Walker
>>> 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?)*
>>>
>>>
>> --
> --Guido (mobile)
>
___
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/K5QNGOF4WOYL6NN2KKDLBGFQKN5THXNJ/
Code of Conduct: http://python.org/psf/codeofconduct/
