On Jun 18, 8:56 pm, Matthew Wilson <m...@tplus1.com> wrote: > Here's the code that I'm feeding to pylint: > > $ cat f.py > from datetime import datetime > > def f(c="today"): > > if c == "today": > c = datetime.today() > > return c.date() > > 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
This is a limitation of static type checking and Pylint is a (limited) static type checker for Python. A language with static type checking would not have even allowed you to compile this. A static type checkers sees this: def midnight_next_day(initial_time [string or in python whatever gets passed(T)]): if initial_time [string (or T)]== [(some constant) string]: initial_time [datetime] = datetime.now() {implied else: initial_time [string or (T)] } return initial_time[could be either datetime or string (oops string does not have date()) pylint doesn’t check for T].date() + timedelta(days=1) or this: def midnight_next_day(initial_time [None object or (T)]): if initial_time [None object or (T)]== [None object]: initial_time [type datetime] = datetime.now() {implied else: initial_time [T] } return initial_time[datetime (pylint doesn’t check for T)].date() + timedelta(days=1) -- http://mail.python.org/mailman/listinfo/python-list