[Tutor] problem with creating paths

Mats Wichmann mats at wichmann.us
Wed Oct 17 13:18:17 EDT 2018


On 10/17/2018 10:07 AM, Shall, Sydney via Tutor wrote:
> Firstly, I would like to thank Steven for reminding me of the assert
> statement. I should have remembered this. It allowed me to isolate the
> problem, which predictably (for me) was very elementary. I am too
> embarrassed to say how simple the error was.
> 
> However, my original problem was not solved by correcting this error.
> 
> So, I will now try and narrow down the location of the problem and then
> if I cannot solve it, I shall return for more good advice.
> 
> Many thanks to Steven and to Peter.

I'll weigh in with a mini- (and unasked-for-) lecture here:

this is often the point at which someone says "boy, I wish Python were
strongly typed, so things didn't change types in flight".

But in fact, the list didn't change types, it's still a list. In fact we
even know where that list is: it's the first element of that tuple you
ended up with. The _name_ you gave to that list carries no typing
meaning, however (although you can give it type hints that an external
tool could use to warn you that you are changing something).  So you
have somewhere given that name to a completely different object, a tuple
which contains your list and another element. So clearly what you're
looking for is the place that happens.

So here's a sketch of how you might use type hinting to find this, to
bring it back to something practical:


=== types.py:
from typing import List, Tuple

a: List[str] = [
    '/a/path',
    '/b/path',
]
print(type(a))
print(a)

a = (a, '/c/path')
print(type(a))
print(a)

=== this works just fine:
$ python3 types.py
<class 'list'>
['/a/path', '/b/path']
<class 'tuple'>
(['/a/path', '/b/path'], '/c/path')

=== but a hinting tool can see a possible issue:
$ mypy types.py
types.py:10: error: Incompatible types in assignment (expression has
type "Tuple[List[str], str]", variable has type "List[str]")




More information about the Tutor mailing list