Skip to content

Commit

Permalink
Fixed TypeError with some VINs (closes idlesign#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
idlesign committed Jul 30, 2020
1 parent 944c394 commit baf37e5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ vininfo changelog
=================


Unreleased
----------
* Fixed TypeError with some VINs (closes #6).


v1.4.0 [2020-05-02]
-------------------
! Dropped support for Py 2.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_renault.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def test_renault():
assert '%s' % vin.brand == 'Renault (Renault)'

details = vin.details
assert not details.engine
assert details.model
assert details.model.code == 'S'
assert details.model.name == ['Logan', 'Sandero', 'Duster', 'Dokker', 'Lodgy']
assert details.body.code == '4'
Expand All @@ -27,3 +29,10 @@ def test_renault():
assert details.transmission.code == '4'
assert details.transmission.name == 'Manual, 5-Gears'
assert details.serial.code == '1234567'


def test_bogus():
vin = Vin('VF1KG1PBE34488860')
details = vin.details
assert details.engine.code == ''
assert not details.engine
20 changes: 16 additions & 4 deletions vininfo/details/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class DetailWrapper:

__slots__ = ['code', 'name']
__slots__ = ['code', 'name', '_supported']

def __init__(self, details: 'VinDetails', detail: 'Detail'):
"""
Expand All @@ -17,22 +17,34 @@ def __init__(self, details: 'VinDetails', detail: 'Detail'):
"""
vin = details._vin
attr_name, attr_idx = detail.source
code_source = getattr(vin, attr_name)

code = code_source[attr_idx]
source = detail.source

code = ''

if source:
attr_name, attr_idx = source
code_source = getattr(vin, attr_name)

code = code_source[attr_idx]

defs = detail.defs

if callable(defs):
defs = defs(details)

self._supported = bool(source)
"""Flag indicating that this detail extraction is available."""

self.code: str = code
self.name: Optional[str] = defs.get(code)

def __str__(self):
return self.name or self.code

def __bool__(self):
return self._supported


class Detail:
"""Vin detail descriptor."""
Expand Down

0 comments on commit baf37e5

Please sign in to comment.