I am trying to store files in a dictionary depending on their type. For this, I am using pygments
API as follow:
# Initialization of the self.files dictionary
self.files = dict()
# Scanning and categorizing the files
for file in files:
lexer = guess_lexer__for_filename(file, None)
if type(lexer) in self.files:
self.files[type(lexer)].append(file)
else:
self.files[type(lexer)] = [file]
But, now, when passing this code through pylint3
, I get a warning telling me that I should use isinstance()
in place of type()
(unidiomatic-typecheck).
The best way to workaround this warning I found so far is as follow:
self.files = dict()
for file in files:
lexer = guess_lexer__for_filename(file, None)
if lexer.__class__ in self.files:
self.files[lexer.__class__].append(file)
else:
self.files[lexer.__class__] = [file]
But, does it really solve the problem ? And, moreover, I started to doubt that using a type as a key in a dictionary is robust enough.
So, is there more suitable and robust ways to do? Any solution with good arguments is welcome.