KEMBAR78
[clean strict optional] Fix some strict optional errors in mypy by ilevkivskyi · Pull Request #3228 · python/mypy · GitHub
Skip to content

Conversation

@ilevkivskyi
Copy link
Member

This PR fixes 26 --strict-optional errors in mypy itself (out of 547 total) in following files:

  • stubgen.py
  • stubgenc.py
  • checkstrformat.py
  • report.py
  • strconv.py


def analyze_conversion_specifiers(self, specifiers: List[ConversionSpecifier],
context: Context) -> bool:
context: Context) -> Optional[bool]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code below distinguishes False and None

xsd_path = os.path.join(reports.data_dir, 'xml', 'mypy.xsd')
self.schema = etree.XMLSchema(etree.parse(xsd_path))
self.last_xml = None # type: etree._ElementTree
self.last_xml = None # type: Optional[etree._ElementTree]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it wouldn't be better to keep this as non-optional and remove the None assignments? Not sure I follow the logic there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the logic correctly, those None assignments are needed, but the attribute should be always non-None at the end, so that I replaced ifs with asserts, I checked locally it works correctly.

mypy/strconv.py Outdated

def __init__(self, show_ids: bool = False) -> None:
self.show_ids = show_ids
self.id_mapper = None # type: IdMapper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not Optional? (Currently mypy ignores the None initialization but we may change that, and it seems to me this function can definitely return with id_mapper still None.

mypy/stubutil.py Outdated


def write_header(file: IO[str], module_name: str, pyversion: Tuple[int, int] = (3, 5)) -> None:
def write_header(file: IO[str], module_name: str = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer Optional[str], EIBTI. (I somewhat regret the rule in PEP 484 about this, or at least I don't enjoy relying on it.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will use Optional[X] (also for previous comment) in this and next PRs. I don't have a preference concerning "syntactic sugar" just would prefer this to be consistent in function annotations and variable annotations (i.e. either both or neither).

I will implement your comments tomorrow, sorry don't have time today.

@gvanrossum gvanrossum merged commit 21f71eb into python:master Apr 26, 2017
@gvanrossum
Copy link
Member

Thanks! Let's remember to mark these files with strict_optional = True in mypy.ini once #3206 lands.

@ilevkivskyi ilevkivskyi deleted the fix-strict-optional-1 branch October 7, 2017 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants