refactor blocklayout.py
-# Copyright (c) 2019-2020, Manfred Moitzi
+# Copyright (c) 2019-2021, Manfred Moitzi
# License: MIT License
-from typing import TYPE_CHECKING, Iterable, Optional, Union
+from typing import Iterable, Optional
from ezdxf.lldxf import const
from .base import BaseLayout
-
-if TYPE_CHECKING:
- from ezdxf.eztypes import DXFGraphic, AttDef
+from ezdxf.entities import DXFGraphic, AttDef, Block, EndBlk
class BlockLayout(BaseLayout):
@@ -14,7 +12,7 @@ class BlockLayout(BaseLayout):
"""
- def __contains__(self, entity: Union['DXFGraphic', str]) -> bool:
+ def __contains__(self, entity) -> bool:
"""Returns ``True`` if block contains `entity`.
Args:
@@ -23,17 +21,18 @@ class BlockLayout(BaseLayout):
"""
if isinstance(entity, str):
entity = self.entitydb[entity]
- return entity in self.entity_space
+ assert isinstance(entity, DXFGraphic)
+ return entity in self.entity_space # type: ignore
@property
- def block(self):
+ def block(self) -> Block:
"""the associated :class:`~ezdxf.entities.Block` entity."""
- return self.block_record.block
+ return self.block_record.block # type: ignore
@property
- def endblk(self):
+ def endblk(self) -> EndBlk:
"""the associated :class:`~ezdxf.entities.EndBlk` entity."""
- return self.block_record.endblk
+ return self.block_record.endblk # type: ignore
@property
def name(self) -> str:
@@ -74,11 +73,11 @@ class BlockLayout(BaseLayout):
def scale_uniformly(self, value: bool):
self.block_record.dxf.scale = int(value)
- def attdefs(self) -> Iterable['AttDef']:
+ def attdefs(self) -> Iterable[AttDef]:
"""Returns iterable of all :class:`~ezdxf.entities.attrib.Attdef`
entities.
"""
- return (entity for entity in self if entity.dxftype() == 'ATTDEF')
+ return (e for e in self if isinstance(e, AttDef))
def has_attdef(self, tag: str) -> bool:
"""Returns ``True`` if an :class:`~ezdxf.entities.attrib.Attdef` for
@@ -86,15 +85,16 @@ class BlockLayout(BaseLayout):
"""
return self.get_attdef(tag) is not None
- def get_attdef(self, tag: str) -> Optional['DXFGraphic']:
+ def get_attdef(self, tag: str) -> Optional[DXFGraphic]:
"""Returns attached :class:`~ezdxf.entities.attrib.Attdef` entity by
`tag` name.
"""
for attdef in self.attdefs():
if tag == attdef.dxf.tag:
return attdef
+ return None
- def get_attdef_text(self, tag: str, default: str = None) -> str:
+ def get_attdef_text(self, tag: str, default: str = "") -> str:
"""Returns text content for :class:`~ezdxf.entities.attrib.Attdef`
`tag` as string or returns `default` if no :class:`Attdef` for `tag`
exist.
@@ -109,9 +109,8 @@ class BlockLayout(BaseLayout):
return default
return attdef.dxf.text
- def get_const_attdefs(self) -> Iterable['AttDef']:
- """ Returns iterable for all constant ATTDEF entities. (internal API)
- """
+ def get_const_attdefs(self) -> Iterable[AttDef]:
+ """Returns iterable for all constant ATTDEF entities. (internal API)"""
return (attdef for attdef in self.attdefs() if attdef.is_const)
def has_non_const_attdef(self) -> bool:
