Skip to content

os.cpu_count - Unsupported operand types for * ("Optional[int]" and "int") in strict mode #4670

@hvnsweeting

Description

@hvnsweeting

The signature of os.cpu_count is Optional[int], which might be not wrong because maybe in some OS, python cannot get the number and returns None.

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

# cat cpucount.py
import os
os.cpu_count() * 5
  • What is the actual behavior/output?
$ mypy cpucount.py  --strict
cpucount.py:2: error: Unsupported operand types for * ("Optional[int]" and "int")
  • What is the behavior/output you expect?

Got the integer result

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Python 3.5.2 mypy 0.560
and also latest mypy on master

cmypy 0.570-dev-21dcd8081bd404b18305796651ecd2d6dd8950ef
  • What are the mypy flags you are using? (For example --strict-optional)
    --strict

Now I understand Optional[int] would accept None or int, all examples I found in doc just use Optional it in function argument, not using that var inside the function body.

I understand that to be strict, it would not allow None + int, but what if my code only run on OSX & Linux, what is the way to go for this error? Writing

# $ cat cpu2.py
import os
cpus = os.cpu_count()
if cpus is None:
    value = 5
else:
    value = cpus * 5

is verbose, and redundant in my case. Or should I just tell mypy ignore it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions