This file is indexed.

/usr/share/doc/python-attr/html/api.html is in python-attr-doc 17.4.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>API Reference &#8212; attrs 17.4.0 documentation</title>
    <link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '17.4.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Extending" href="extending.html" />
    <link rel="prev" title="attrs by Example" href="examples.html" />
   
  <link rel="stylesheet" href="_static/custom.css" type="text/css" />
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="api-reference">
<span id="api"></span><h1>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline"></a></h1>
<p><code class="docutils literal"><span class="pre">attrs</span></code> works by decorating a class using <a class="reference internal" href="#attr.s" title="attr.s"><code class="xref py py-func docutils literal"><span class="pre">attr.s()</span></code></a> and then optionally defining attributes on the class using <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When this documentation speaks about “<code class="docutils literal"><span class="pre">attrs</span></code> attributes” it means those attributes that are defined using <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a> in the class body.</p>
</div>
<p>What follows is the API explanation, if you’d like a more hands-on introduction, have a look at <a class="reference internal" href="examples.html"><span class="doc">attrs by Example</span></a>.</p>
<div class="section" id="core">
<h2>Core<a class="headerlink" href="#core" title="Permalink to this headline"></a></h2>
<dl class="function">
<dt id="attr.s">
<code class="descclassname">attr.</code><code class="descname">s</code><span class="sig-paren">(</span><em>these=None</em>, <em>repr_ns=None</em>, <em>repr=True</em>, <em>cmp=True</em>, <em>hash=None</em>, <em>init=True</em>, <em>slots=False</em>, <em>frozen=False</em>, <em>str=False</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.s" title="Permalink to this definition"></a></dt>
<dd><p>A class decorator that adds <a class="reference external" href="https://wiki.python.org/moin/DunderAlias">dunder</a>-methods according to the
specified attributes using <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a> or the <em>these</em> argument.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>these</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#dict" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">dict</span></code></a> of <a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#str" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> to <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a>) – <p>A dictionary of name to <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a> mappings.  This is
useful to avoid the definition of your attributes within the class body
because you can’t (e.g. if you want to add <code class="docutils literal"><span class="pre">__repr__</span></code> methods to
Django models) or don’t want to.</p>
<p>If <em>these</em> is not <code class="docutils literal"><span class="pre">None</span></code>, <code class="docutils literal"><span class="pre">attrs</span></code> will <em>not</em> search the class body
for attributes.</p>
</li>
<li><strong>repr_ns</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#str" title="(in Python v3.6)"><em>str</em></a>) – When using nested classes, there’s no way in Python 2
to automatically detect that.  Therefore it’s possible to set the
namespace explicitly for a more meaningful <code class="docutils literal"><span class="pre">repr</span></code> output.</li>
<li><strong>repr</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Create a <code class="docutils literal"><span class="pre">__repr__</span></code> method with a human readable
representation of <code class="docutils literal"><span class="pre">attrs</span></code> attributes..</li>
<li><strong>str</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Create a <code class="docutils literal"><span class="pre">__str__</span></code> method that is identical to
<code class="docutils literal"><span class="pre">__repr__</span></code>.  This is usually not necessary except for
<a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#Exception" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">Exception</span></code></a>s.</li>
<li><strong>cmp</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Create <code class="docutils literal"><span class="pre">__eq__</span></code>, <code class="docutils literal"><span class="pre">__ne__</span></code>, <code class="docutils literal"><span class="pre">__lt__</span></code>, <code class="docutils literal"><span class="pre">__le__</span></code>,
<code class="docutils literal"><span class="pre">__gt__</span></code>, and <code class="docutils literal"><span class="pre">__ge__</span></code> methods that compare the class as if it were
a tuple of its <code class="docutils literal"><span class="pre">attrs</span></code> attributes.  But the attributes are <em>only</em>
compared, if the type of both classes is <em>identical</em>!</li>
<li><strong>hash</strong> (<code class="docutils literal"><span class="pre">bool</span></code> or <code class="docutils literal"><span class="pre">None</span></code>) – <p>If <code class="docutils literal"><span class="pre">None</span></code> (default), the <code class="docutils literal"><span class="pre">__hash__</span></code> method is generated
according how <em>cmp</em> and <em>frozen</em> are set.</p>
<ol class="arabic">
<li>If <em>both</em> are True, <code class="docutils literal"><span class="pre">attrs</span></code> will generate a <code class="docutils literal"><span class="pre">__hash__</span></code> for you.</li>
<li>If <em>cmp</em> is True and <em>frozen</em> is False, <code class="docutils literal"><span class="pre">__hash__</span></code> will be set to
None, marking it unhashable (which it is).</li>
<li>If <em>cmp</em> is False, <code class="docutils literal"><span class="pre">__hash__</span></code> will be left untouched meaning the
<code class="docutils literal"><span class="pre">__hash__</span></code> method of the superclass will be used (if superclass is
<code class="docutils literal"><span class="pre">object</span></code>, this means it will fall back to id-based hashing.).</li>
</ol>
<p>Although not recommended, you can decide for yourself and force
<code class="docutils literal"><span class="pre">attrs</span></code> to create one (e.g. if the class is immutable even though you
didn’t freeze it programmatically) by passing <code class="docutils literal"><span class="pre">True</span></code> or not.  Both of
these cases are rather special and should be used carefully.</p>
<p>See the <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__hash__">Python documentation </a>
and the <a class="reference external" href="https://github.com/python-attrs/attrs/issues/136">GitHub issue that led to the default behavior </a> for more details.</p>
</li>
<li><strong>init</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Create a <code class="docutils literal"><span class="pre">__init__</span></code> method that initializes the
<code class="docutils literal"><span class="pre">attrs</span></code> attributes.  Leading underscores are stripped for the
argument name.  If a <code class="docutils literal"><span class="pre">__attrs_post_init__</span></code> method exists on the
class, it will be called after the class is fully initialized.</li>
<li><strong>slots</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Create a <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#slots">slots</a>-style class that’s more
memory-efficient.  See <a class="reference internal" href="examples.html#slots"><span class="std std-ref">Slots</span></a> for further ramifications.</li>
<li><strong>frozen</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – <p>Make instances immutable after initialization.  If
someone attempts to modify a frozen instance,
<a class="reference internal" href="#attr.exceptions.FrozenInstanceError" title="attr.exceptions.FrozenInstanceError"><code class="xref py py-exc docutils literal"><span class="pre">attr.exceptions.FrozenInstanceError</span></code></a> is raised.</p>
<p>Please note:</p>
<blockquote>
<div><ol class="arabic">
<li>This is achieved by installing a custom <code class="docutils literal"><span class="pre">__setattr__</span></code> method
on your class so you can’t implement an own one.</li>
<li>True immutability is impossible in Python.</li>
<li>This <em>does</em> have a minor a runtime performance <a class="reference internal" href="how-does-it-work.html#how-frozen"><span class="std std-ref">impact</span></a> when initializing new instances.  In other words:
<code class="docutils literal"><span class="pre">__init__</span></code> is slightly slower with <code class="docutils literal"><span class="pre">frozen=True</span></code>.</li>
<li>If a class is frozen, you cannot modify <code class="docutils literal"><span class="pre">self</span></code> in
<code class="docutils literal"><span class="pre">__attrs_post_init__</span></code> or a self-written <code class="docutils literal"><span class="pre">__init__</span></code>. You can
circumvent that limitation by using
<code class="docutils literal"><span class="pre">object.__setattr__(self,</span> <span class="pre">&quot;attribute_name&quot;,</span> <span class="pre">value)</span></code>.</li>
</ol>
</div></blockquote>
</li>
<li><strong>auto_attribs</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – <p>If True, collect <a class="reference external" href="https://www.python.org/dev/peps/pep-0526/">PEP 526</a>-annotated attributes
(Python 3.6 and later only) from the class body.</p>
<p>In this case, you <strong>must</strong> annotate every field.  If <code class="docutils literal"><span class="pre">attrs</span></code>
encounters a field that is set to an <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a> but lacks a type
annotation, an <a class="reference internal" href="#attr.exceptions.UnannotatedAttributeError" title="attr.exceptions.UnannotatedAttributeError"><code class="xref py py-exc docutils literal"><span class="pre">attr.exceptions.UnannotatedAttributeError</span></code></a> is
raised.  Use <code class="docutils literal"><span class="pre">field_name:</span> <span class="pre">typing.Any</span> <span class="pre">=</span> <span class="pre">attr.ib(...)</span></code> if you don’t
want to set a type.</p>
<p>If you assign a value to those attributes (e.g. <code class="docutils literal"><span class="pre">x:</span> <span class="pre">int</span> <span class="pre">=</span> <span class="pre">42</span></code>), that
value becomes the default value like if it were passed using
<code class="docutils literal"><span class="pre">attr.ib(default=42)</span></code>.  Passing an instance of <a class="reference internal" href="#attr.Factory" title="attr.Factory"><code class="xref py py-class docutils literal"><span class="pre">Factory</span></code></a> also
works as expected.</p>
<p>Attributes annotated as <a class="reference external" href="/usr/share/doc/python3-doc/html/library/typing.html#typing.ClassVar" title="(in Python v3.6)"><code class="xref py py-data docutils literal"><span class="pre">typing.ClassVar</span></code></a> are <strong>ignored</strong>.</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.0.0: </span><em>slots</em></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.1.0: </span><em>frozen</em></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.3.0: </span><em>str</em>, and support for <code class="docutils literal"><span class="pre">__attrs_post_init__</span></code>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 17.1.0: </span><em>hash</em> supports <code class="docutils literal"><span class="pre">None</span></code> as value which is also the default
now.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.3.0: </span><em>auto_attribs</em></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal"><span class="pre">attrs</span></code> also comes with a serious business alias <code class="docutils literal"><span class="pre">attr.attrs</span></code>.</p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">attr</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">_private</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">private</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="go">C(_private=42)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">D</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">D</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">&lt;D object at ...&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">D</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">these</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;x&quot;</span><span class="p">:</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()},</span> <span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)(</span><span class="n">D</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">D</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">D(x=1)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.ib">
<code class="descclassname">attr.</code><code class="descname">ib</code><span class="sig-paren">(</span><em>default=NOTHING</em>, <em>validator=None</em>, <em>repr=True</em>, <em>cmp=True</em>, <em>hash=None</em>, <em>init=True</em>, <em>convert=None</em>, <em>metadata=None</em>, <em>type=None</em>, <em>converter=None</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.ib" title="Permalink to this definition"></a></dt>
<dd><p>Create a new attribute on a class.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Does <em>not</em> do anything unless the class is also decorated with
<a class="reference internal" href="#attr.s" title="attr.s"><code class="xref py py-func docutils literal"><span class="pre">attr.s()</span></code></a>!</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>default</strong> (<em>Any value.</em>) – <p>A value that is used if an <code class="docutils literal"><span class="pre">attrs</span></code>-generated <code class="docutils literal"><span class="pre">__init__</span></code>
is used and no value is passed while instantiating or the attribute is
excluded using <code class="docutils literal"><span class="pre">init=False</span></code>.</p>
<p>If the value is an instance of <a class="reference internal" href="#attr.Factory" title="attr.Factory"><code class="xref py py-class docutils literal"><span class="pre">Factory</span></code></a>, its callable will be
used to construct a new value (useful for mutable data types like lists
or dicts).</p>
<p>If a default is not set (or set manually to <code class="docutils literal"><span class="pre">attr.NOTHING</span></code>), a value
<em>must</em> be supplied when instantiating; otherwise a <a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a>
will be raised.</p>
<p>The default can also be set using decorator notation as shown below.</p>
</li>
<li><strong>validator</strong> (<code class="docutils literal"><span class="pre">callable</span></code> or a <code class="docutils literal"><span class="pre">list</span></code> of <code class="docutils literal"><span class="pre">callable</span></code>s.) – <p><a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">callable()</span></code></a> that is called by <code class="docutils literal"><span class="pre">attrs</span></code>-generated
<code class="docutils literal"><span class="pre">__init__</span></code> methods after the instance has been initialized.  They
receive the initialized instance, the <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">Attribute</span></code></a>, and the
passed value.</p>
<p>The return value is <em>not</em> inspected so the validator has to throw an
exception itself.</p>
<p>If a <code class="docutils literal"><span class="pre">list</span></code> is passed, its items are treated as validators and must
all pass.</p>
<p>Validators can be globally disabled and re-enabled using
<a class="reference internal" href="#attr.get_run_validators" title="attr.get_run_validators"><code class="xref py py-func docutils literal"><span class="pre">get_run_validators()</span></code></a>.</p>
<p>The validator can also be set using decorator notation as shown below.</p>
</li>
<li><strong>repr</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Include this attribute in the generated <code class="docutils literal"><span class="pre">__repr__</span></code>
method.</li>
<li><strong>cmp</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Include this attribute in the generated comparison methods
(<code class="docutils literal"><span class="pre">__eq__</span></code> et al).</li>
<li><strong>hash</strong> (<code class="docutils literal"><span class="pre">bool</span></code> or <code class="docutils literal"><span class="pre">None</span></code>) – Include this attribute in the generated <code class="docutils literal"><span class="pre">__hash__</span></code>
method.  If <code class="docutils literal"><span class="pre">None</span></code> (default), mirror <em>cmp</em>’s value.  This is the
correct behavior according the Python spec.  Setting this value to
anything else than <code class="docutils literal"><span class="pre">None</span></code> is <em>discouraged</em>.</li>
<li><strong>init</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Include this attribute in the generated <code class="docutils literal"><span class="pre">__init__</span></code>
method.  It is possible to set this to <code class="docutils literal"><span class="pre">False</span></code> and set a default
value.  In that case this attributed is unconditionally initialized
with the specified default value or factory.</li>
<li><strong>converter</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><em>callable</em></a>) – <a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">callable()</span></code></a> that is called by
<code class="docutils literal"><span class="pre">attrs</span></code>-generated <code class="docutils literal"><span class="pre">__init__</span></code> methods to converter attribute’s value
to the desired format.  It is given the passed-in value, and the
returned value will be used as the new value of the attribute.  The
value is converted before being passed to the validator, if any.</li>
<li><strong>metadata</strong> – An arbitrary mapping, to be used by third-party
components.  See <a class="reference internal" href="extending.html#extending-metadata"><span class="std std-ref">Metadata</span></a>.</li>
<li><strong>type</strong><p>The type of the attribute.  In Python 3.6 or greater, the
preferred method to specify the type is using a variable annotation
(see <a class="reference external" href="https://www.python.org/dev/peps/pep-0526/">PEP 526</a>).
This argument is provided for backward compatibility.
Regardless of the approach used, the type will be stored on
<code class="docutils literal"><span class="pre">Attribute.type</span></code>.</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 15.2.0: </span><em>convert</em></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.3.0: </span><em>metadata</em></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 17.1.0: </span><em>validator</em> can be a <code class="docutils literal"><span class="pre">list</span></code> now.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 17.1.0: </span><em>hash</em> is <code class="docutils literal"><span class="pre">None</span></code> and therefore mirrors <em>cmp</em> by default.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.3.0: </span><em>type</em></p>
</div>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 17.4.0: </span><em>convert</em></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.4.0: </span><em>converter</em> as a replacement for the deprecated
<em>convert</em> to achieve consistency with other noun-based arguments.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal"><span class="pre">attrs</span></code> also comes with a serious business alias <code class="docutils literal"><span class="pre">attr.attrib</span></code>.</p>
</div>
<p>The object returned by <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a> also allows for setting the default and the validator using decorators:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="nd">@x</span><span class="o">.</span><span class="n">validator</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">name_can_be_anything</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">value</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;x must be positive&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="nd">@y</span><span class="o">.</span><span class="n">default</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">name_does_not_matter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">C(x=1, y=2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">ValueError</span>: <span class="n">x must be positive</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="attr.Attribute">
<em class="property">class </em><code class="descclassname">attr.</code><code class="descname">Attribute</code><span class="sig-paren">(</span><em>name</em>, <em>default</em>, <em>validator</em>, <em>repr</em>, <em>cmp</em>, <em>hash</em>, <em>init</em>, <em>convert=None</em>, <em>metadata=None</em>, <em>type=None</em>, <em>converter=None</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.Attribute" title="Permalink to this definition"></a></dt>
<dd><p><em>Read-only</em> representation of an attribute.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Attribute name:</th><td class="field-body">The name of the attribute.</td>
</tr>
</tbody>
</table>
<p>Plus <em>all</em> arguments of <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a>.</p>
<p>For the version history of the fields, see <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a>.</p>
<p>Instances of this class are frequently used for introspection purposes like:</p>
<ul class="simple">
<li><a class="reference internal" href="#attr.fields" title="attr.fields"><code class="xref py py-func docutils literal"><span class="pre">fields()</span></code></a> returns a tuple of them.</li>
<li>Validators get them passed as the first argument.</li>
</ul>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">You should never instantiate this class yourself!</p>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">attr</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">x</span>
<span class="go">Attribute(name=&#39;x&#39;, default=NOTHING, validator=None, repr=True, cmp=True, hash=None, init=True, metadata=mappingproxy({}), type=None, converter=None)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.make_class">
<code class="descclassname">attr.</code><code class="descname">make_class</code><span class="sig-paren">(</span><em>name</em>, <em>attrs</em>, <em>bases=(&lt;class 'object'&gt;</em>, <em>)</em>, <em>**attributes_arguments</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.make_class" title="Permalink to this definition"></a></dt>
<dd><p>A quick way to create a new class called <em>name</em> with <em>attrs</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>name</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#str" title="(in Python v3.6)"><em>str</em></a>) – The name for the new class.</li>
<li><strong>attrs</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#list" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">list</span></code></a> or <a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#dict" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">dict</span></code></a>) – A list of names or a dictionary of mappings of names to
attributes.</li>
<li><strong>bases</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#tuple" title="(in Python v3.6)"><em>tuple</em></a>) – Classes that the new class will subclass.</li>
<li><strong>attributes_arguments</strong> – Passed unmodified to <a class="reference internal" href="#attr.s" title="attr.s"><code class="xref py py-func docutils literal"><span class="pre">attr.s()</span></code></a>.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">A new class with <em>attrs</em>.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last"><a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#type" title="(in Python v3.6)">type</a></p>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.1.0: </span><em>bases</em></p>
</div>
<p>This is handy if you want to programmatically create classes.</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C1</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">make_class</span><span class="p">(</span><span class="s2">&quot;C1&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C1</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">C1(x=1, y=2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C2</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">make_class</span><span class="p">(</span><span class="s2">&quot;C2&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s2">&quot;x&quot;</span><span class="p">:</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">),</span>
<span class="gp">... </span>                            <span class="s2">&quot;y&quot;</span><span class="p">:</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">Factory</span><span class="p">(</span><span class="nb">list</span><span class="p">))})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C2</span><span class="p">()</span>
<span class="go">C2(x=42, y=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="attr.Factory">
<em class="property">class </em><code class="descclassname">attr.</code><code class="descname">Factory</code><span class="sig-paren">(</span><em>factory</em>, <em>takes_self=False</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.Factory" title="Permalink to this definition"></a></dt>
<dd><p>Stores a factory callable.</p>
<p>If passed as the default value to <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a>, the factory is used to
generate a new value.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>factory</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><em>callable</em></a>) – A callable that takes either none or exactly one
mandatory positional argument depending on <em>takes_self</em>.</li>
<li><strong>takes_self</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Pass the partially initialized instance that is
being initialized as a positional argument.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.1.0: </span><em>takes_self</em></p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">Factory</span><span class="p">(</span><span class="nb">list</span><span class="p">))</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">Factory</span><span class="p">(</span>
<span class="gp">... </span>        <span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">),</span>
<span class="gp">... </span>        <span class="n">takes_self</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span>    <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">()</span>
<span class="go">C(x=[], y=set())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="go">C(x=[1, 2, 3], y={1, 2, 3})</span>
</pre></div>
</div>
</dd></dl>

<dl class="exception">
<dt id="attr.exceptions.FrozenInstanceError">
<em class="property">exception </em><code class="descclassname">attr.exceptions.</code><code class="descname">FrozenInstanceError</code><a class="headerlink" href="#attr.exceptions.FrozenInstanceError" title="Permalink to this definition"></a></dt>
<dd><p>A frozen/immutable instance has been attempted to be modified.</p>
<p>It mirrors the behavior of <code class="docutils literal"><span class="pre">namedtuples</span></code> by using the same error message
and subclassing <a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#AttributeError" title="(in Python v3.6)"><code class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.1.0.</span></p>
</div>
</dd></dl>

<dl class="exception">
<dt id="attr.exceptions.AttrsAttributeNotFoundError">
<em class="property">exception </em><code class="descclassname">attr.exceptions.</code><code class="descname">AttrsAttributeNotFoundError</code><a class="headerlink" href="#attr.exceptions.AttrsAttributeNotFoundError" title="Permalink to this definition"></a></dt>
<dd><p>An <code class="docutils literal"><span class="pre">attrs</span></code> function couldn’t find an attribute that the user asked for.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.2.0.</span></p>
</div>
</dd></dl>

<dl class="exception">
<dt id="attr.exceptions.NotAnAttrsClassError">
<em class="property">exception </em><code class="descclassname">attr.exceptions.</code><code class="descname">NotAnAttrsClassError</code><a class="headerlink" href="#attr.exceptions.NotAnAttrsClassError" title="Permalink to this definition"></a></dt>
<dd><p>A non-<code class="docutils literal"><span class="pre">attrs</span></code> class has been passed into an <code class="docutils literal"><span class="pre">attrs</span></code> function.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.2.0.</span></p>
</div>
</dd></dl>

<dl class="exception">
<dt id="attr.exceptions.DefaultAlreadySetError">
<em class="property">exception </em><code class="descclassname">attr.exceptions.</code><code class="descname">DefaultAlreadySetError</code><a class="headerlink" href="#attr.exceptions.DefaultAlreadySetError" title="Permalink to this definition"></a></dt>
<dd><p>A default has been set using <code class="docutils literal"><span class="pre">attr.ib()</span></code> and is attempted to be reset
using the decorator.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.1.0.</span></p>
</div>
</dd></dl>

<dl class="exception">
<dt id="attr.exceptions.UnannotatedAttributeError">
<em class="property">exception </em><code class="descclassname">attr.exceptions.</code><code class="descname">UnannotatedAttributeError</code><a class="headerlink" href="#attr.exceptions.UnannotatedAttributeError" title="Permalink to this definition"></a></dt>
<dd><p>A class with <code class="docutils literal"><span class="pre">auto_attribs=True</span></code> has an <code class="docutils literal"><span class="pre">attr.ib()</span></code> without a type
annotation.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.3.0.</span></p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">auto_attribs</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
    <span class="n">x</span><span class="p">:</span> <span class="nb">int</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<div class="section" id="influencing-initialization">
<h3>Influencing Initialization<a class="headerlink" href="#influencing-initialization" title="Permalink to this headline"></a></h3>
<p>Generally speaking, it’s best to keep logic out of your <code class="docutils literal"><span class="pre">__init__</span></code>.
The moment you need a finer control over how your class is instantiated, it’s usually best to use a classmethod factory or to apply the <a class="reference external" href="https://en.wikipedia.org/wiki/Builder_pattern">builder pattern</a>.</p>
<p>However, sometimes you need to do that one quick thing after your class is initialized.
And for that <code class="docutils literal"><span class="pre">attrs</span></code> offers the <code class="docutils literal"><span class="pre">__attrs_post_init__</span></code> hook that is automatically detected and run after <code class="docutils literal"><span class="pre">attrs</span></code> is done initializing your instance:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__attrs_post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">C(x=1, y=2)</span>
</pre></div>
</div>
<p>Please note that you can’t directly set attributes on frozen classes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">frozen</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">FrozenBroken</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__attrs_post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">FrozenBroken</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">attr.exceptions.FrozenInstanceError</span>: <span class="n">can&#39;t set attribute</span>
</pre></div>
</div>
<p>If you need to set attributes on a frozen class, you’ll have to resort to the <a class="reference internal" href="how-does-it-work.html#how-frozen"><span class="std std-ref">same trick</span></a> as <code class="docutils literal"><span class="pre">attrs</span></code> and use <a class="reference external" href="/usr/share/doc/python3-doc/html/reference/datamodel.html#object.__setattr__" title="(in Python v3.6)"><code class="xref py py-meth docutils literal"><span class="pre">object.__setattr__()</span></code></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">frozen</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Frozen</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__attrs_post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">object</span><span class="o">.</span><span class="fm">__setattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Frozen</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">Frozen(x=1, y=2)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="helpers">
<span id="id2"></span><h2>Helpers<a class="headerlink" href="#helpers" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal"><span class="pre">attrs</span></code> comes with a bunch of helper methods that make working with it easier:</p>
<dl class="function">
<dt id="attr.fields">
<code class="descclassname">attr.</code><code class="descname">fields</code><span class="sig-paren">(</span><em>cls</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.fields" title="Permalink to this definition"></a></dt>
<dd><p>Returns the tuple of <code class="docutils literal"><span class="pre">attrs</span></code> attributes for a class.</p>
<p>The tuple also allows accessing the fields by their names (see below for
examples).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><p class="first"><strong>cls</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#type" title="(in Python v3.6)"><em>type</em></a>) – Class to introspect.</p>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Raises:</th><td class="field-body"><ul class="first simple">
<li><a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><strong>TypeError</strong></a> – If <em>cls</em> is not a class.</li>
<li><a class="reference internal" href="#attr.exceptions.NotAnAttrsClassError" title="attr.exceptions.NotAnAttrsClassError"><strong>attr.exceptions.NotAnAttrsClassError</strong></a> – If <em>cls</em> is not an <code class="docutils literal"><span class="pre">attrs</span></code>
class.</li>
</ul>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">tuple (with name accessors) of <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a></p>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 16.2.0: </span>Returned tuple allows accessing the fields
by name.</p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
<span class="go">(Attribute(name=&#39;x&#39;, default=NOTHING, validator=None, repr=True, cmp=True, hash=None, init=True, metadata=mappingproxy({}), type=None, converter=None), Attribute(name=&#39;y&#39;, default=NOTHING, validator=None, repr=True, cmp=True, hash=None, init=True, metadata=mappingproxy({}), type=None, converter=None))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">Attribute(name=&#39;y&#39;, default=NOTHING, validator=None, repr=True, cmp=True, hash=None, init=True, metadata=mappingproxy({}), type=None, converter=None)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">y</span> <span class="ow">is</span> <span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.has">
<code class="descclassname">attr.</code><code class="descname">has</code><span class="sig-paren">(</span><em>cls</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.has" title="Permalink to this definition"></a></dt>
<dd><p>Check whether <em>cls</em> is a class with <code class="docutils literal"><span class="pre">attrs</span></code> attributes.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>cls</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#type" title="(in Python v3.6)"><em>type</em></a>) – Class to introspect.</td>
</tr>
<tr class="field-even field"><th class="field-name">Raises:</th><td class="field-body"><a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><strong>TypeError</strong></a> – If <em>cls</em> is not a class.</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">bool</span></code></a></td>
</tr>
</tbody>
</table>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">pass</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">has</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">has</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.asdict">
<code class="descclassname">attr.</code><code class="descname">asdict</code><span class="sig-paren">(</span><em>inst</em>, <em>recurse=True</em>, <em>filter=None</em>, <em>dict_factory=&lt;class 'dict'&gt;</em>, <em>retain_collection_types=False</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.asdict" title="Permalink to this definition"></a></dt>
<dd><p>Return the <code class="docutils literal"><span class="pre">attrs</span></code> attribute values of <em>inst</em> as a dict.</p>
<p>Optionally recurse into other <code class="docutils literal"><span class="pre">attrs</span></code>-decorated classes.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>inst</strong> – Instance of an <code class="docutils literal"><span class="pre">attrs</span></code>-decorated class.</li>
<li><strong>recurse</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Recurse into classes that are also
<code class="docutils literal"><span class="pre">attrs</span></code>-decorated.</li>
<li><strong>filter</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><em>callable</em></a>) – A callable whose return code determines whether an
attribute or element is included (<code class="docutils literal"><span class="pre">True</span></code>) or dropped (<code class="docutils literal"><span class="pre">False</span></code>).  Is
called with the <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a> as the first argument and the
value as the second argument.</li>
<li><strong>dict_factory</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><em>callable</em></a>) – A callable to produce dictionaries from.  For
example, to produce ordered dictionaries instead of normal Python
dictionaries, pass in <code class="docutils literal"><span class="pre">collections.OrderedDict</span></code>.</li>
<li><strong>retain_collection_types</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Do not convert to <code class="docutils literal"><span class="pre">list</span></code> when
encountering an attribute whose type is <code class="docutils literal"><span class="pre">tuple</span></code> or <code class="docutils literal"><span class="pre">set</span></code>.  Only
meaningful if <code class="docutils literal"><span class="pre">recurse</span></code> is <code class="docutils literal"><span class="pre">True</span></code>.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first">return type of <em>dict_factory</em></p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body"><p class="first last"><a class="reference internal" href="#attr.exceptions.NotAnAttrsClassError" title="attr.exceptions.NotAnAttrsClassError"><strong>attr.exceptions.NotAnAttrsClassError</strong></a> – If <em>cls</em> is not an <code class="docutils literal"><span class="pre">attrs</span></code>
class.</p>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.0.0: </span><em>dict_factory</em></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.1.0: </span><em>retain_collection_types</em></p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">asdict</span><span class="p">(</span><span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">C</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)))</span>
<span class="go">{&#39;x&#39;: 1, &#39;y&#39;: {&#39;x&#39;: 2, &#39;y&#39;: 3}}</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.astuple">
<code class="descclassname">attr.</code><code class="descname">astuple</code><span class="sig-paren">(</span><em>inst</em>, <em>recurse=True</em>, <em>filter=None</em>, <em>tuple_factory=&lt;class 'tuple'&gt;</em>, <em>retain_collection_types=False</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.astuple" title="Permalink to this definition"></a></dt>
<dd><p>Return the <code class="docutils literal"><span class="pre">attrs</span></code> attribute values of <em>inst</em> as a tuple.</p>
<p>Optionally recurse into other <code class="docutils literal"><span class="pre">attrs</span></code>-decorated classes.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>inst</strong> – Instance of an <code class="docutils literal"><span class="pre">attrs</span></code>-decorated class.</li>
<li><strong>recurse</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Recurse into classes that are also
<code class="docutils literal"><span class="pre">attrs</span></code>-decorated.</li>
<li><strong>filter</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><em>callable</em></a>) – A callable whose return code determines whether an
attribute or element is included (<code class="docutils literal"><span class="pre">True</span></code>) or dropped (<code class="docutils literal"><span class="pre">False</span></code>).  Is
called with the <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a> as the first argument and the
value as the second argument.</li>
<li><strong>tuple_factory</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><em>callable</em></a>) – A callable to produce tuples from.  For
example, to produce lists instead of tuples.</li>
<li><strong>retain_collection_types</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#bool" title="(in Python v3.6)"><em>bool</em></a>) – Do not convert to <code class="docutils literal"><span class="pre">list</span></code>
or <code class="docutils literal"><span class="pre">dict</span></code> when encountering an attribute which type is
<code class="docutils literal"><span class="pre">tuple</span></code>, <code class="docutils literal"><span class="pre">dict</span></code> or <code class="docutils literal"><span class="pre">set</span></code>.  Only meaningful if <code class="docutils literal"><span class="pre">recurse</span></code> is
<code class="docutils literal"><span class="pre">True</span></code>.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first">return type of <em>tuple_factory</em></p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body"><p class="first last"><a class="reference internal" href="#attr.exceptions.NotAnAttrsClassError" title="attr.exceptions.NotAnAttrsClassError"><strong>attr.exceptions.NotAnAttrsClassError</strong></a> – If <em>cls</em> is not an <code class="docutils literal"><span class="pre">attrs</span></code>
class.</p>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 16.2.0.</span></p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">astuple</span><span class="p">(</span><span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="go">(1, 2)</span>
</pre></div>
</div>
</dd></dl>

<p><code class="docutils literal"><span class="pre">attrs</span></code> includes some handy helpers for filtering:</p>
<dl class="function">
<dt id="attr.filters.include">
<code class="descclassname">attr.filters.</code><code class="descname">include</code><span class="sig-paren">(</span><em>*what</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.filters.include" title="Permalink to this definition"></a></dt>
<dd><p>Whitelist <em>what</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>what</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#list" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">list</span></code></a> of <a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#type" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">type</span></code></a> or <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a>s) – What to whitelist.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><code class="xref py py-class docutils literal"><span class="pre">callable</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="attr.filters.exclude">
<code class="descclassname">attr.filters.</code><code class="descname">exclude</code><span class="sig-paren">(</span><em>*what</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.filters.exclude" title="Permalink to this definition"></a></dt>
<dd><p>Blacklist <em>what</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>what</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#list" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">list</span></code></a> of classes or <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a>s.) – What to blacklist.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><code class="xref py py-class docutils literal"><span class="pre">callable</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>

<p>See <a class="reference internal" href="examples.html#asdict"><span class="std std-ref">Converting to Collections Types</span></a> for examples.</p>
<dl class="function">
<dt id="attr.evolve">
<code class="descclassname">attr.</code><code class="descname">evolve</code><span class="sig-paren">(</span><em>inst</em>, <em>**changes</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.evolve" title="Permalink to this definition"></a></dt>
<dd><p>Create a new instance, based on <em>inst</em> with <em>changes</em> applied.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>inst</strong> – Instance of a class with <code class="docutils literal"><span class="pre">attrs</span></code> attributes.</li>
<li><strong>changes</strong> – Keyword changes in the new copy.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">A copy of inst with <em>changes</em> incorporated.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body"><ul class="first last simple">
<li><a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><strong>TypeError</strong></a> – If <em>attr_name</em> couldn’t be found in the class
<code class="docutils literal"><span class="pre">__init__</span></code>.</li>
<li><a class="reference internal" href="#attr.exceptions.NotAnAttrsClassError" title="attr.exceptions.NotAnAttrsClassError"><strong>attr.exceptions.NotAnAttrsClassError</strong></a> – If <em>cls</em> is not an <code class="docutils literal"><span class="pre">attrs</span></code>
class.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.1.0.</span></p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span>    <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i1</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i1</span>
<span class="go">C(x=1, y=2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i2</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">i1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i2</span>
<span class="go">C(x=1, y=3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i1</span> <span class="o">==</span> <span class="n">i2</span>
<span class="go">False</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">evolve</span></code> creates a new instance using <code class="docutils literal"><span class="pre">__init__</span></code>.
This fact has several implications:</p>
<ul class="simple">
<li>private attributes should be specified without the leading underscore, just like in <code class="docutils literal"><span class="pre">__init__</span></code>.</li>
<li>attributes with <code class="docutils literal"><span class="pre">init=False</span></code> can’t be set with <code class="docutils literal"><span class="pre">evolve</span></code>.</li>
<li>the usual <code class="docutils literal"><span class="pre">__init__</span></code> validators will validate the new values.</li>
</ul>
</dd></dl>

<dl class="function">
<dt id="attr.validate">
<code class="descclassname">attr.</code><code class="descname">validate</code><span class="sig-paren">(</span><em>inst</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.validate" title="Permalink to this definition"></a></dt>
<dd><p>Validate all attributes on <em>inst</em> that have a validator.</p>
<p>Leaves all exceptions through.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>inst</strong> – Instance of a class with <code class="docutils literal"><span class="pre">attrs</span></code> attributes.</td>
</tr>
</tbody>
</table>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">instance_of</span><span class="p">(</span><span class="nb">int</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="s2">&quot;1&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">attr</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">(&quot;&#39;x&#39; must be &lt;type &#39;int&#39;&gt; (got &#39;1&#39; that is a &lt;type &#39;str&#39;&gt;).&quot;, Attribute(name=&#39;x&#39;, default=NOTHING, validator=&lt;instance_of validator for type &lt;type &#39;int&#39;&gt;&gt;, repr=True, cmp=True, hash=None, init=True, type=None), &lt;type &#39;int&#39;&gt;, &#39;1&#39;)</span>
</pre></div>
</div>
</dd></dl>

<p>Validators can be globally disabled if you want to run them only in development and tests but not in production because you fear their performance impact:</p>
<dl class="function">
<dt id="attr.set_run_validators">
<code class="descclassname">attr.</code><code class="descname">set_run_validators</code><span class="sig-paren">(</span><em>run</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.set_run_validators" title="Permalink to this definition"></a></dt>
<dd><p>Set whether or not validators are run.  By default, they are run.</p>
</dd></dl>

<dl class="function">
<dt id="attr.get_run_validators">
<code class="descclassname">attr.</code><code class="descname">get_run_validators</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#attr.get_run_validators" title="Permalink to this definition"></a></dt>
<dd><p>Return whether or not validators are run.</p>
</dd></dl>

</div>
<div class="section" id="validators">
<span id="api-validators"></span><h2>Validators<a class="headerlink" href="#validators" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal"><span class="pre">attrs</span></code> comes with some common validators in the <code class="docutils literal"><span class="pre">attrs.validators</span></code> module:</p>
<dl class="function">
<dt id="attr.validators.instance_of">
<code class="descclassname">attr.validators.</code><code class="descname">instance_of</code><span class="sig-paren">(</span><em>type</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.validators.instance_of" title="Permalink to this definition"></a></dt>
<dd><p>A validator that raises a <a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> if the initializer is called
with a wrong type for this particular attribute (checks are performed using
<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#isinstance" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">isinstance()</span></code></a> therefore it’s also valid to pass a tuple of types).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>type</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#type" title="(in Python v3.6)"><em>type</em></a><em> or </em><em>tuple of types</em>) – The type to check for.</td>
</tr>
<tr class="field-even field"><th class="field-name">Raises:</th><td class="field-body"><a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><strong>TypeError</strong></a> – With a human readable error message, the attribute
(of type <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a>), the expected type, and the value it
got.</td>
</tr>
</tbody>
</table>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">instance_of</span><span class="p">(</span><span class="nb">int</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">C(x=42)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="s2">&quot;42&quot;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">(&quot;&#39;x&#39; must be &lt;type &#39;int&#39;&gt; (got &#39;42&#39; that is a &lt;type &#39;str&#39;&gt;).&quot;, Attribute(name=&#39;x&#39;, default=NOTHING, validator=&lt;instance_of validator for type &lt;type &#39;int&#39;&gt;&gt;, type=None), &lt;type &#39;int&#39;&gt;, &#39;42&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">(&quot;&#39;x&#39; must be &lt;type &#39;int&#39;&gt; (got None that is a &lt;type &#39;NoneType&#39;&gt;).&quot;, Attribute(name=&#39;x&#39;, default=NOTHING, validator=&lt;instance_of validator for type &lt;type &#39;int&#39;&gt;&gt;, repr=True, cmp=True, hash=None, init=True, type=None), &lt;type &#39;int&#39;&gt;, None)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.validators.in_">
<code class="descclassname">attr.validators.</code><code class="descname">in_</code><span class="sig-paren">(</span><em>options</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.validators.in_" title="Permalink to this definition"></a></dt>
<dd><p>A validator that raises a <a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#ValueError" title="(in Python v3.6)"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> if the initializer is called
with a value that does not belong in the options provided.  The check is
performed using <code class="docutils literal"><span class="pre">value</span> <span class="pre">in</span> <span class="pre">options</span></code>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>options</strong> (list, tuple, <a class="reference external" href="/usr/share/doc/python3-doc/html/library/enum.html#enum.Enum" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">enum.Enum</span></code></a>, …) – Allowed options.</td>
</tr>
<tr class="field-even field"><th class="field-name">Raises:</th><td class="field-body"><a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#ValueError" title="(in Python v3.6)"><strong>ValueError</strong></a> – With a human readable error message, the attribute (of
type <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a>), the expected options, and the value it
got.</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.1.0.</span></p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">enum</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">State</span><span class="p">(</span><span class="n">enum</span><span class="o">.</span><span class="n">Enum</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">ON</span> <span class="o">=</span> <span class="s2">&quot;on&quot;</span>
<span class="gp">... </span>    <span class="n">OFF</span> <span class="o">=</span> <span class="s2">&quot;off&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">state</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">in_</span><span class="p">(</span><span class="n">State</span><span class="p">))</span>
<span class="gp">... </span>    <span class="n">val</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">in_</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">State</span><span class="o">.</span><span class="n">ON</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">C(state=&lt;State.ON: &#39;on&#39;&gt;, val=1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="s2">&quot;on&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">&#39;state&#39; must be in &lt;enum &#39;State&#39;&gt; (got &#39;on&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">State</span><span class="o">.</span><span class="n">ON</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">&#39;val&#39; must be in [1, 2, 3] (got 4)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.validators.provides">
<code class="descclassname">attr.validators.</code><code class="descname">provides</code><span class="sig-paren">(</span><em>interface</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.validators.provides" title="Permalink to this definition"></a></dt>
<dd><p>A validator that raises a <a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> if the initializer is called
with an object that does not provide the requested <em>interface</em> (checks are
performed using <code class="docutils literal"><span class="pre">interface.providedBy(value)</span></code> (see <a class="reference external" href="https://zopeinterface.readthedocs.io/en/latest/">zope.interface</a>).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>interface</strong> (<em>zope.interface.Interface</em>) – The interface to check for.</td>
</tr>
<tr class="field-even field"><th class="field-name">Raises:</th><td class="field-body"><a class="reference external" href="/usr/share/doc/python3-doc/html/library/exceptions.html#TypeError" title="(in Python v3.6)"><strong>TypeError</strong></a> – With a human readable error message, the attribute
(of type <a class="reference internal" href="#attr.Attribute" title="attr.Attribute"><code class="xref py py-class docutils literal"><span class="pre">attr.Attribute</span></code></a>), the expected interface, and the
value it got.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="attr.validators.and_">
<code class="descclassname">attr.validators.</code><code class="descname">and_</code><span class="sig-paren">(</span><em>*validators</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.validators.and_" title="Permalink to this definition"></a></dt>
<dd><p>A validator that composes multiple validators into one.</p>
<p>When called on a value, it runs all wrapped validators.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>validators</strong> (<em>callables</em>) – Arbitrary number of validators.</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.1.0.</span></p>
</div>
<p>For convenience, it’s also possible to pass a list to <a class="reference internal" href="#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a>’s validator argument.</p>
<p>Thus the following two statements are equivalent:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">and_</span><span class="p">(</span><span class="n">v1</span><span class="p">,</span> <span class="n">v2</span><span class="p">,</span> <span class="n">v3</span><span class="p">))</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="p">[</span><span class="n">v1</span><span class="p">,</span> <span class="n">v2</span><span class="p">,</span> <span class="n">v3</span><span class="p">])</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="attr.validators.optional">
<code class="descclassname">attr.validators.</code><code class="descname">optional</code><span class="sig-paren">(</span><em>validator</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.validators.optional" title="Permalink to this definition"></a></dt>
<dd><p>A validator that makes an attribute optional.  An optional attribute is one
which can be set to <code class="docutils literal"><span class="pre">None</span></code> in addition to satisfying the requirements of
the sub-validator.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>validator</strong> (callable or <a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#list" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">list</span></code></a> of callables.) – A validator (or a list of validators) that is used for
non-<code class="docutils literal"><span class="pre">None</span></code> values.</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 15.1.0.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 17.1.0: </span><em>validator</em> can be a list of validators.</p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">optional</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">instance_of</span><span class="p">(</span><span class="nb">int</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">C(x=42)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="s2">&quot;42&quot;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
   <span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">(&quot;&#39;x&#39; must be &lt;type &#39;int&#39;&gt; (got &#39;42&#39; that is a &lt;type &#39;str&#39;&gt;).&quot;, Attribute(name=&#39;x&#39;, default=NOTHING, validator=&lt;instance_of validator for type &lt;type &#39;int&#39;&gt;&gt;, type=None), &lt;type &#39;int&#39;&gt;, &#39;42&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="go">C(x=None)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="converters">
<h2>Converters<a class="headerlink" href="#converters" title="Permalink to this headline"></a></h2>
<dl class="function">
<dt id="attr.converters.optional">
<code class="descclassname">attr.converters.</code><code class="descname">optional</code><span class="sig-paren">(</span><em>converter</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.converters.optional" title="Permalink to this definition"></a></dt>
<dd><p>A converter that allows an attribute to be optional. An optional attribute
is one which can be set to <code class="docutils literal"><span class="pre">None</span></code>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>converter</strong> (<a class="reference external" href="/usr/share/doc/python3-doc/html/library/functions.html#callable" title="(in Python v3.6)"><em>callable</em></a>) – the converter that is used for non-<code class="docutils literal"><span class="pre">None</span></code>
values.</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 17.1.0.</span></p>
</div>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">converter</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">converters</span><span class="o">.</span><span class="n">optional</span><span class="p">(</span><span class="nb">int</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="go">C(x=None)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">C(x=42)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="deprecated-apis">
<h2>Deprecated APIs<a class="headerlink" href="#deprecated-apis" title="Permalink to this headline"></a></h2>
<p>The serious business aliases used to be called <code class="docutils literal"><span class="pre">attr.attributes</span></code> and <code class="docutils literal"><span class="pre">attr.attr</span></code>.
There are no plans to remove them but they shouldn’t be used in new code.</p>
<dl class="function">
<dt id="attr.assoc">
<code class="descclassname">attr.</code><code class="descname">assoc</code><span class="sig-paren">(</span><em>inst</em>, <em>**changes</em><span class="sig-paren">)</span><a class="headerlink" href="#attr.assoc" title="Permalink to this definition"></a></dt>
<dd><p>Copy <em>inst</em> and apply <em>changes</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>inst</strong> – Instance of a class with <code class="docutils literal"><span class="pre">attrs</span></code> attributes.</li>
<li><strong>changes</strong> – Keyword changes in the new copy.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">A copy of inst with <em>changes</em> incorporated.</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body"><ul class="first last simple">
<li><a class="reference internal" href="#attr.exceptions.AttrsAttributeNotFoundError" title="attr.exceptions.AttrsAttributeNotFoundError"><strong>attr.exceptions.AttrsAttributeNotFoundError</strong></a> – If <em>attr_name</em> couldn’t
be found on <em>cls</em>.</li>
<li><a class="reference internal" href="#attr.exceptions.NotAnAttrsClassError" title="attr.exceptions.NotAnAttrsClassError"><strong>attr.exceptions.NotAnAttrsClassError</strong></a> – If <em>cls</em> is not an <code class="docutils literal"><span class="pre">attrs</span></code>
class.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 17.1.0: </span>Use <a class="reference internal" href="#attr.evolve" title="attr.evolve"><code class="xref py py-func docutils literal"><span class="pre">evolve()</span></code></a> instead.</p>
</div>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/attrs_logo.svg" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">API Reference</a><ul>
<li><a class="reference internal" href="#core">Core</a><ul>
<li><a class="reference internal" href="#influencing-initialization">Influencing Initialization</a></li>
</ul>
</li>
<li><a class="reference internal" href="#helpers">Helpers</a></li>
<li><a class="reference internal" href="#validators">Validators</a></li>
<li><a class="reference internal" href="#converters">Converters</a></li>
<li><a class="reference internal" href="#deprecated-apis">Deprecated APIs</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="examples.html" title="previous chapter"><code class="docutils literal"><span class="pre">attrs</span></code> by Example</a></li>
      <li>Next: <a href="extending.html" title="next chapter">Extending</a></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2018, Hynek Schlawack.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.7</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
      
    </div>

    

    
  </body>
</html>