@@ -345,50 +345,176 @@ narrow down the type to a specific type:
345
345
f(' x' ) # OK
346
346
f(1.1 ) # Error
347
347
348
- .. _ optional :
348
+ .. _ strict_optional :
349
349
350
- The type of None and optional types
351
- ***********************************
350
+ Optional types and the None type
351
+ ********************************
352
352
353
- Mypy treats the type of ``None `` as special. ``None `` is a valid value
354
- for every type, which resembles ``null `` in Java. Unlike Java, mypy
355
- doesn't treat primitives types
356
- specially: ``None `` is also valid for primitive types such as ``int ``
357
- and ``float ``.
353
+ You can use the ``Optional `` type modifier to define a type variant
354
+ that includes ``None ``, such as ``Optional[int] ``:
358
355
359
- .. note ::
356
+ .. code-block :: python
357
+
358
+ from typing import Optional
359
+
360
+ def f () -> Optional[int ]:
361
+ return None # OK
362
+
363
+ def g () -> int :
364
+ ...
365
+ return None # Error: None not compatible with int
366
+
367
+ You can equivalently use ``Union[str, None] `` -- but ``Optional `` is
368
+ shorter and more idiomatic. In this case ``None `` is a type with only
369
+ one value, ``None ``. Most operations will not be allowed on unguarded
370
+ ``None `` or ``Optional `` values:
371
+
372
+ .. code-block :: python
360
373
361
- See :ref: `strict_optional ` for an experimental mode which allows
362
- mypy to check ``None `` values precisely.
374
+ def my_inc (x : Optional[int ]) -> int :
375
+ return x + 1 # Error: Cannot add None and int
376
+
377
+ Instead, an explicit ``None `` check is required. Mypy has
378
+ powerful type inference that lets you use regular Python
379
+ idioms to guard against ``None `` values. For example, mypy
380
+ recognizes ``is None `` checks:
381
+
382
+ .. code-block :: python
383
+
384
+ def my_inc (x : Optional[int ]) -> int :
385
+ if x is None :
386
+ return 0
387
+ else :
388
+ # The inferred type of x is just int here.
389
+ return x + 1
390
+
391
+ Mypy will infer the type of ``x `` to be ``int `` in the else block due to the
392
+ check against ``None `` in the if condition.
393
+
394
+ Other supported checks for guarding against a ``None `` value include ``if x is None ``,
395
+ ``if x `` and ``if not x ``. Additionally, mypy understands ``None `` checks within
396
+ logical expressions:
397
+
398
+ .. code-block :: python
399
+
400
+ def concat (x : Optional[str ], y : Optional[str ]) -> Optional[str ]:
401
+ if x and y:
402
+ # Both x and y are not None here
403
+ return x + y
404
+ else :
405
+ return None
406
+
407
+ Sometimes mypy doesn't realize that a value is never ``None ``. This notably
408
+ happens when a class instance can exist in a partially defined state,
409
+ where some attribute is initialized to ``None `` during object
410
+ construction, but a method assumes that the attribute is no longer ``None ``. Mypy
411
+ will complain about the possible ``None `` value. You can use
412
+ ``assert x is not None `` to work around this:
413
+
414
+ .. code-block :: python
415
+
416
+ class Resource :
417
+ path: Optional[str ] = None
418
+
419
+ def initialize (self , path : str ) -> None :
420
+ self .path = path
421
+
422
+ def read (self ) -> str :
423
+ # We require that the object has been initizlied.
424
+ assert self .path is not None
425
+ with open (self .path) as f: # OK
426
+ return f.read()
427
+
428
+ r = Resource()
429
+ r.initialize(' /foo/bar' )
430
+ r.read()
363
431
364
432
When initializing a variable as ``None ``, ``None `` is usually an
365
433
empty place-holder value, and the actual value has a different type.
366
- This is why you need to annotate an attribute in a case like this:
434
+ This is why you need to annotate an attribute in a cases like the class
435
+ ``Resource `` above:
367
436
368
437
.. code-block :: python
369
438
370
- class A :
439
+ class Resource :
440
+ path: Optional[str ] = None
441
+ ...
442
+
443
+ This also works for attributes defined within methods:
444
+
445
+ .. code-block :: python
446
+
447
+ class Counter :
371
448
def __init__ (self ) -> None :
372
- self .count = None # type: int
449
+ self .count: Optional[int ] = None
450
+
451
+ As a special case, you can use a non-optional type when initializing an
452
+ attribute to ``None `` inside a class body *and * using a type comment,
453
+ since when using a type comment, an initializer is syntacticaly required,
454
+ and ``None `` is used as a dummy, placeholder initializer:
373
455
374
- Mypy will complain if you omit the type annotation, as it wouldn't be
375
- able to infer a non-trivial type for the ``count `` attribute
376
- otherwise.
456
+ .. code-block :: python
457
+
458
+ from typing import List
459
+
460
+ class Container :
461
+ items = None # type: List [ str ] # OK (only with type comment)
377
462
378
463
Mypy generally uses the first assignment to a variable to
379
464
infer the type of the variable. However, if you assign both a ``None ``
380
- value and a non-``None `` value in the same scope, mypy can often do
381
- the right thing:
465
+ value and a non-``None `` value in the same scope, mypy can usually do
466
+ the right thing without an annotation :
382
467
383
468
.. code-block :: python
384
469
385
470
def f (i : int ) -> None :
386
- n = None # Inferred type int because of the assignment below
471
+ n = None # Inferred type Optional[ int] because of the assignment below
387
472
if i > 0 :
388
473
n = i
389
474
...
390
475
391
- Often it's useful to know whether a variable can be
476
+ .. note ::
477
+
478
+ ``None `` is also used as the return type for functions that don't
479
+ return a value, i.e. that implicitly return ``None ``. (Mypy doesn't
480
+ use ``NoneType `` for this, since it would
481
+ look awkward, even though that is the real name of the type of ``None ``
482
+ -- try ``type(None) `` in the interactive interpreter to see for yourself.)
483
+
484
+ .. note ::
485
+
486
+ ``Optional[...] `` *does not * mean a function argument with a default value.
487
+ However, if the default value of an argument is ``None ``, you can use
488
+ an optional type for the argument.
489
+
490
+ .. _no_strict_optional :
491
+
492
+ Disabling strict None checking
493
+ ******************************
494
+
495
+ Mypy also has an option to treat ``None `` as a valid value for every
496
+ type (in case you know Java, it's useful to think of it as similar to
497
+ the Java ``null ``). In this mode ``None `` is also valid for primitive
498
+ types such as ``int `` and ``float ``.
499
+
500
+ The mode is enabled through the ``--no-strict-optional `` command-line
501
+ option. It will cause mypy to silently accept some buggy code, such as
502
+ this example, so it's not recommended if you can avoid it:
503
+
504
+ .. code-block :: python
505
+
506
+ def inc (x : int ) -> int :
507
+ return x + 1
508
+
509
+ x = inc(None ) # No error reported by mypy!
510
+
511
+ In mypy versions before 0.600 this was the default mode, and if you
512
+ recently upgraded to a more recent mypy version, you can enable this
513
+ option for backward compatibility, if you don't want to introduce
514
+ optional types to your codebase yet. Making code "optional clean"
515
+ can take some work!
516
+
517
+ Often it's still useful to document whether a variable can be
392
518
``None ``. For example, this function accepts a ``None `` argument,
393
519
but it's not obvious from its signature:
394
520
@@ -403,8 +529,9 @@ but it's not obvious from its signature:
403
529
print (greeting(' Python' )) # Okay!
404
530
print (greeting(None )) # Also okay!
405
531
406
- Mypy lets you use ``Optional[t] `` to document that ``None `` is a
407
- valid argument type:
532
+ You can still use ``Optional[t] `` to document that ``None `` is a
533
+ valid argument type, even if strict ``None `` checking is not
534
+ enabled:
408
535
409
536
.. code-block :: python
410
537
@@ -418,66 +545,8 @@ valid argument type:
418
545
419
546
Mypy treats this as semantically equivalent to the previous example,
420
547
since ``None `` is implicitly valid for any type, but it's much more
421
- useful for a programmer who is reading the code. You can equivalently
422
- use ``Union[str, None] ``, but ``Optional `` is shorter and more
423
- idiomatic.
424
-
425
- .. note ::
426
-
427
- ``None `` is also used as the return type for functions that don't
428
- return a value, i.e. that implicitly return ``None ``. Mypy doesn't
429
- use ``NoneType `` for this, since it would
430
- look awkward, even though that is the real name of the type of ``None ``
431
- (try ``type(None) `` in the interactive interpreter to see for yourself).
432
-
433
- .. _strict_optional :
434
-
435
- Strict optional type and None checking
436
- ***************************************************
437
-
438
- Currently, ``None `` is a valid value for each type, similar to
439
- ``null `` or ``NULL `` in many languages. However, you can use the
440
- ``--strict-optional `` command line option
441
- (which will become the default in the near future)
442
- to tell mypy that types should not include ``None ``
443
- by default. The ``Optional `` type modifier is then used to define
444
- a type variant that includes ``None ``, such as ``Optional[int] ``:
445
-
446
- .. code-block :: python
447
-
448
- from typing import Optional
449
-
450
- def f () -> Optional[int ]:
451
- return None # OK
452
-
453
- def g () -> int :
454
- ...
455
- return None # Error: None not compatible with int
456
-
457
- Also, most operations will not be allowed on unguarded ``None ``
458
- or ``Optional `` values:
459
-
460
- .. code-block :: python
461
-
462
- def f (x : Optional[int ]) -> int :
463
- return x + 1 # Error: Cannot add None and int
464
-
465
- Instead, an explicit ``None `` check is required. Mypy has
466
- powerful type inference that lets you use regular Python
467
- idioms to guard against ``None `` values. For example, mypy
468
- recognizes ``is None `` checks:
469
-
470
- .. code-block :: python
471
-
472
- def f (x : Optional[int ]) -> int :
473
- if x is None :
474
- return 0
475
- else :
476
- # The inferred type of x is just int here.
477
- return x + 1
478
-
479
- Mypy will infer the type of ``x `` to be ``int `` in the else block due to the
480
- check against ``None `` in the if condition.
548
+ useful for a programmer who is reading the code. This also makes
549
+ it easier to migrate to strict ``None `` checking in the future.
481
550
482
551
.. _noreturn :
483
552
0 commit comments