Skip to content

Commit

Permalink
Add a conditional branch to issue a warning
Browse files Browse the repository at this point in the history
when adding duplicate members to `EnumerationNamespaces.add`.
Also, added cases to the examples that trigger a warning.
This is related to #550.
  • Loading branch information
junkmd committed Sep 28, 2024
1 parent 4b09af9 commit 7b3caab
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions comtypes/tools/codegenerator/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Optional, Union as _UnionT
from typing import Dict, List, Set, Tuple
from typing import Iterator, Mapping, Sequence
import warnings


class ImportedNamespaces(object):
Expand Down Expand Up @@ -168,7 +169,13 @@ def add(self, enum_name: str, member_name: str, value: int) -> None:
>>> enums.add('Foo', 'spam', 2)
>>> enums.add('Bar', 'bacon', 3)
>>> enums.add('Bar', 'egg', 4)
>>> enums.add('Bar', 'egg', 5)
>>> import warnings
>>> with warnings.catch_warnings(record=True) as w:
... enums.add('Bar', 'egg', 5)
... print(w[-1].message.args[0].replace(', ', ',\\n'))
The 'egg' member of the 'Bar' enumeration is already assigned 4,
but it will be overwritten with 5,
based on the type information.
>>> assert 'Foo' in enums
>>> assert 'Baz' not in enums
>>> print(enums.to_intflags())
Expand All @@ -193,7 +200,20 @@ class Bar(IntFlag):
egg = 5 # duplicated within the 'Bar'. Perhaps there is a bug?
Bar = c_int # enum
"""
self.data.setdefault(enum_name, []).append((member_name, value))
members = self.data.setdefault(enum_name, [])
if members:
mapping = dict(members)
if member_name in mapping:
# This may be a bug in the COM type library.
# See also https://github.com/enthought/comtypes/issues/550
warnings.warn(
f"The '{member_name}' member of the '{enum_name}' enumeration "
f"is already assigned {mapping[member_name]}, "
f"but it will be overwritten with {value}, "
"based on the type information.",
UserWarning,
)
members.append((member_name, value))

def __contains__(self, item: str) -> bool:
return item in self.data
Expand Down

0 comments on commit 7b3caab

Please sign in to comment.