Commit Graph

43 Commits

Author SHA1 Message Date
wouter bolsterlee 0b913645c5 support custom repr() callable for attributes (#568)
* wip: support custom repr() callable for attributes

see #567

* extend ‘repr=...’ arg type in .pyi stubs

* expand docstring for attr.ib()

* add changelog entry

* add docs with example

* improve my copy/paste skills 🙈

* fix grammar

* fix typo in changelog entry

* fix and improve attrib() docstring

* detect custom repr() once, not per call. be strict about bool.

* use rst syntax, not markdown

* apply hynek's suggestions for changelog entry

* add ‘versionchanged’ note in docstring

* add custom attribute repr= to typing example

* simplify comment
2019-09-06 14:26:08 +02:00
David Euresti eda9f2de6f Make unique_filename not depend on repr. (#560)
* Make unique_filename not depend on repr.

For debugging purposes, when a method is created it needs to be put
into a "filename".  This filename was generated using `repr(attrs)`
however this meant that the filename could change every time the
program was loaded depending on the whims of repr.  (In particular
free functions and lambdas would include ids)

This solves the problem by changing the name to be <attrs generated
{method} {module}.{qualname}> If it should happen that the name is
taken then -2 will be appended and incremented until a free filename
is found.

* Add tests

* Fix tests and changelog
2019-08-01 12:42:13 +02:00
Hynek Schlawack 4b88f924d6 Remove _add_init
More concretely: move it to tests. It was dead code for a while however its
unittests are still valuable as the exercise quite a bit of _make_init.
2019-02-27 07:24:24 +01:00
Hynek Schlawack 1fada3d374 Be consistent in calling slotted classes slotted 2019-02-25 16:09:57 +01:00
Hynek Schlawack 3421b4bd97 Split test and fix warning
message is deprecated.
2019-02-02 14:36:56 +01:00
Ryan Gabbard d2be76c964 Clear cache hash on de-serialization (#489)
* Don't cache hash codes across deserialization.

 Because the hash code cache field gets serialized and deserialized by
 Pickle, previously when you deserialize a cache_hash=True attrs object, the
 hashcode will be the hashcode the object had at serialization-time.
 However, if your object had fields with hash codes which were not
 deterministic between interpreter runs, then on a new interpreter run
 your deserialized object would have a hash code which differs from a
 newly created identical object.

 This commit fixes that by clearing the cache on deserialization. It
 needs to override the __setstate__ method to do so, so this commit
 also forbids using a custom __setstate__ on a cache_hash=True object.

Closes #482 .

* Improve exception type

* Minor tweaks to comments formatting

* Fix test for Python 2.7

* Fix error in comment

* Make nomenclature consistent for slotted/dict classes

* Remove hasattr()

* Improve comments and error message for custom __setstate__ + cache_hash=True

* Drop use of  for test classes for cache hash serialization

* Make changelog note this is a breaking change

* Make exception message point to tracking issue

* Fix test classes for Python 2.7
2019-02-02 14:32:01 +01:00
Ryan Gabbard 3e0ecbd891 Cache hash codes (#426)
* First stab at implementing hashcode caching (#423)

Currently all existing tests pass but no cache_hash tests have yet
been added.

* Existing hash tests now pass on cache_hash classes

* Add towncrier change log

* Add documentation for cache_hash

* Fixes bug with check that init=True if cache_hash=True

* Fix long lines

* Fix documentation issues

* Add test for cache_hash requiring init

* Improve test coverage

* Remove now unnecessary 'pass'

* Add periods to the end of exception strings

* Add test docstrings for cache_hash tests

* Clarify documentation of cache_hash

* Recommend that hashable classes be frozen

* Fix test references for exception messages
2018-08-20 06:46:52 +02:00
Hynek Schlawack a372d51b7b
Fade to Black (#392) 2018-06-10 19:40:07 +02:00
Glyph 5e46afda49
return "..." rather than raising RecursionError for repr() on a cyclic object
Fixes #95
2018-03-17 01:32:28 -07:00
Yann Kaiser 9d441d70ab Fix __repr__ crashing when a member is uninitialized or deleted (#308) 2017-12-06 10:21:58 -08:00
Hynek Schlawack ef9a062022
Use isort so we can stop thinking about imports (#297) 2017-11-26 22:18:07 +01:00
Hynek Schlawack 03c10ad0ef Use new serious biz names in internal code (#257)
* Use new serious biz names

* Use offical API in tests where possible

* Fix flake8
2017-10-02 19:32:10 +02:00
Hynek Schlawack 8b490b8586 Stop setting attributes on class bodies (#253)
* Stop setting attributes on class bodies

This behavior has been deprecated since 16.1 and can now be removed in
accordance with our backward-compatibility policy.

* We don't need iterkeys anymore
2017-10-02 12:14:37 +02:00
Andy Freeland a6cf2d5637 Fix unbound __str__ on Python 2 when slots=True (#199)
Previously:

```pycon
>>> import attr
>>> @attr.s(str=True, slots=True)
... class A(object):
...     a = attr.ib()
...
>>> a = A(1)
>>> str(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method repr_() must be called with A instance as first argument (got nothing instead)
```

Fixes #198.
2017-06-01 09:30:31 +02:00
Hynek Schlawack f560500886 Fix hashing behavior (#142) 2017-02-19 09:51:43 +01:00
Hynek Schlawack b0942e1b2e Add str argument to @attr.s
Fixes #112
2016-11-20 15:52:11 +01:00
Tin Tvrtković 0fac9215b8 Introduce attribute metadata. (#96) 2016-11-19 09:47:03 +01:00
Hynek Schlawack cfa6d2e99f Implement immutability (#60) 2016-08-20 18:45:15 +02:00
Hynek Schlawack b337f5b9d5 Move test helpers into an utils module
__init__.py should never contain any code.
2016-08-15 15:27:13 +02:00
Hynek Schlawack 4b94e0314c Fix _Nothing class' __eq__
All instances used to be equal to anything.  Didn't pop up because attrs
always compares them using `is`.
2016-04-28 10:32:38 +02:00
Tin Tvrtkovic 821e8537af We hypothesizin'. 2016-03-23 00:15:14 +01:00
Tin Tvrtkovic 37bc4b878c Rename class_ to cls.
Fix docstrings.
Fix missing parens in a test.
2016-03-17 19:40:11 +01:00
Tin Tvrtkovic 7b201eb199 Initial slots implementation. 2016-03-14 03:02:13 +01:00
Hynek Schlawack 174d37f941 Allow mandatory attributes after init=False attrs
Maybe not the best design but a valid case nonetheless.
2016-02-06 12:20:37 +01:00
Hynek Schlawack 1af3eaa877 Allow the case of initializing attributes that are set to `init=False`
This allows for clean initializer parameter lists while being able to
initialize attributes to default values.
2015-12-19 13:43:07 +01:00
Hynek Schlawack 788ade3bcf Fix flake8 2015-07-03 12:15:05 +02:00
Hynek Schlawack 0149b538ab Fix tests on Python 2.6
Fixes #14 and #15.
2015-07-03 11:30:53 +02:00
Hynek Schlawack fba942270b Stay happy and positive
Closes #6
2015-02-20 13:29:47 +01:00
Hynek Schlawack 980067d8f1 We're ASCII-clean 2015-02-20 11:22:10 +01:00
Hynek Schlawack 08ff764de7 Pass instances into validators
Also construct the instance first and then run all validators on it.

This allows for cross-attribute validation.
2015-02-09 13:16:56 +01:00
Hynek Schlawack 4e81b7b5e2 Rename add_XXX to no_XXX
Assume you want everything by default and be consistent with the no_XXX
attributes on Attribute.
2015-02-07 09:15:59 +01:00
Hynek Schlawack e7ebf4bf81 Allow single attributes to be excluded 2015-01-30 17:48:34 +01:00
Hynek Schlawack 943d686509 Validate assignments 2015-01-30 13:30:47 +01:00
Hynek Schlawack f61fb5d185 Eliminate factory field
Instead, have only one default field.  If the default value is an instance of
attr.Factory, use it as a factory.
2015-01-29 23:10:56 +01:00
Hynek Schlawack 717dcdadca Short default_value to default 2015-01-29 22:32:41 +01:00
Hynek Schlawack 9cb48a5814 Shorten default_factory to factory 2015-01-29 22:29:37 +01:00
Hynek Schlawack 8f98b38136 Increase branch coverage
And a lot of under the hood work.
2015-01-29 17:31:49 +01:00
Hynek Schlawack 3e45f0559e Strip underscore for __init__
There is not such thing as private parameters.
2015-01-29 13:05:04 +01:00
Hynek Schlawack 692e4613a0 Add instance_of validator 2015-01-29 12:20:17 +01:00
Hynek Schlawack 774cf0142a Pass the Attribute object into validators
This allows for more generic validators.
2015-01-29 09:22:06 +01:00
Hynek Schlawack 372c55318c Add validators 2015-01-28 15:54:41 +01:00
Hynek Schlawack 58d5c0a767 Get rid of <> in repr
Closes #1
2015-01-27 23:08:55 +01:00
Hynek Schlawack 9560908555 Initial commit
Docs aren't there yet
2015-01-27 17:53:17 +01:00