Matthew Wilson wrote:
Here's the code that I'm feeding to pylint:

    $ cat f.py
    from datetime import datetime

    def f(c="today"):

pylint infers that you intend users to pass a string. Human would guess the same at this point.

        if c == "today":
                    c = datetime.today()

Now I guess that you actually intend c to be passed as a datetime object. You only used the string as a type annotation, not as a real default value. Something like 'record_date = None' is better.

        return c.date()

and here you ask for the input's date, which strings do not have.


And here's what pylint says:

    $ pylint -e f.py
    No config file found, using default configuration
    ************* Module f
    E: 10:f: Instance of 'str' has no 'date' member (but some types could
    not be inferred)

Is this a valid error message?  Is the code above bad?  If so, what is
the right way?

I changed from using a string as the default to None, and then pylint
didn't mind:


$ cat f.py from datetime import datetime

    def f(c=None):

        if c is None:
                    c = datetime.today()

        return c.date()

$ pylint -e f.py No config file found, using default configuration

I don't see any difference between using a string vs None.  Both are
immutable.  I find the string much more informative, since I can write
out what I want.

Looking for comments.

Matt

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to