[Tutor] Unnecessary comprehension warning

boB Stepp robertvstepp at gmail.com
Sun Jun 7 00:15:03 EDT 2020


On Sat, Jun 6, 2020 at 10:14 PM DL Neil via Tutor <tutor at python.org> wrote:
>
> On 7/06/20 3:00 PM, boB Stepp wrote:
> > On Sat, Jun 6, 2020 at 9:29 PM DL Neil via Tutor <tutor at python.org> wrote:
> >>
> >> On 7/06/20 2:19 PM, boB Stepp wrote:
> >>> Continuing "Exercise 3.12: Using your library module" in "3.4 Modules" at
> >>> https://dabeaz-course.github.io/practical-python/Notes/03_Program_organization/04_Modules.html.
> >>>
> >>>
> >>> I rewrote another function in report.py, read_prices(), to make use of the
> >>> parse_csv() function in the fileparse module.  The rewritten function is:
> >>>
> >>> def read_prices(filename: str) -> Dict[str, float]:
> >>>       """Read a file of stock prices and load them into a dictionary."""
> >>>       prices = fileparse.parse_csv(filename, types=[str, float],
> >>> has_headers=False)
> >>>       stock_prices = {stock_name: stock_price for stock_name, stock_price
> >>> in prices}
> >>>       return stock_prices
> >>>
> >>> pylint complains with:
> >>>
> >>> report.py|25 col 1 warning| unnecessary-comprehension: Unnecessary use
> >>> of a comprehension
> >>>
> >>> The dictionary comprehension I'm now using replaces a lengthier for loop
> >>> with indexing instead of nice names.  Am I truly doing things in a bad
> >>> Python style as the linter is complaining?  Should I be doing this
> >>> differently?
> >>
> >>
> >> The name "prices" is defined and then used, but enjoys no further reference.
> >>
> >> What happens (from PyLint's PoV) if you make the two lines into one?
> >> (overly-complicated and ugly though it would be)
> >
> > Strangely, it makes no difference, same warning.
>
>
> After fileparse, what is type( prices )?

type(prices) = <class 'list'>


-- 
boB


More information about the Tutor mailing list